3

所以我使用以下脚本:

area <- c(1854,2001,2182,2520,4072,1627,1308,1092,854,1223,2231,1288,898,2328,1660,6018,5420,943,1625,1095,1484,929,1178,4072,2413)
weight1 <- c(24281,28474,33725,40707,76124,16263,12190,10153,8631,13690,34408,15375,8806,36245,20506,109489,104014,11308,23262,11778,20650,8771,12356,76124,28346)
weight <- weight1/1000

df <- data.frame(weight = log10(weight), area = log10(area))

fit_line <- predict(lm(area ~ weight, data=df))
fit_power <- predict(nls(area ~ i*weight^z, start=list(i=2,z=0.7), data=df))

plot(df$weight,df$area)
lines(df$weight,fit_line,col="red")
lines(sort(df$weight),sort(fit_power), col="blue")

做一个对数 - 对数图。我可以绘制一条直线,lm()但是当我nls()用来进行功率拟合时,它绘制的是曲线而不是直线,见下文:

在此处输入图像描述

如何以直线的形式绘制功率拟合,或者如何从lm(). 所以我有以下形式的答案:y = a*x^b

4

2 回答 2

2

你的情节不是对数情节。做一个对数图:

plot(log(area)~log(weight), df)

然后拟合一条线:

LM.Log <- lm(log(area)~log(weight), df)
abline(LM.Log, col="red")

并更有效地通过直线绘制曲线:

Power <- coef(LM.Log)[2]
LM.Normal <- lm(area~I(weight^Power)+0, df)
plot(area~weight, df)
plot(function(x) coef(LM.Normal)*x^Power, 0, 2, add=T, col="blue")
于 2012-12-19T20:00:22.297 回答
1

也许以下内容会有所启发......

df <- data.frame(weight, area, weightl = log10(weight), areal = log10(area))
df <- df[order(df$weight),]

fit_line <- predict(lm(areal ~ weightl, data=df))
fit_power <- predict(nls(area ~ i*weight^z, start=list(i=2,z=0.7), data=df))

plot(df$weightl, df$areal)
lines(df$weightl, fit_line, col="red")
lines(df$weightl, log10(fit_power), col="blue")

plot(df$weight, df$area)
lines(df$weight, 10^fit_line, col="red")
lines(df$weight, fit_power, col="blue")

我猜想,我希望是正确的,你真的想要一个通过原始值的功率曲线,你把 log10 作为这样的代理。因此,您需要做的是获取原始重量/面积关系的预测值,然后记录这些值并将所有内容放在日志图上。或者获取对数值的线性并将它们都作为曲线放在原始图上。检查上面生成的两个图。

于 2012-12-19T19:52:37.957 回答