绘制直线的力量而不是曲线

时间:2012-12-19 18:47:30

标签: r

所以我使用以下脚本:

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()进行幂拟合时,它会绘制曲线而不是直线,请参见下文:

enter image description here

如何以直线的形式绘制幂拟合,或者如何从lm()导出它。所以我得到了以下形式的答案:y = a * x ^ b

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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作为代理。因此,您需要做的是获取原始重量/面积关系的预测值,然后记录这些并将所有内容放在日志图上。或者获取对数值的线性值,并将它们作为曲线放在原始图形上。检查上面生成的两个图。