我正在尝试拟合非线性回归后的曲线。
这是我的数据集:
stem_diameter <- c(15, 15, 16, 17, 19, 23, 23, 24, 24, 25, 25, 26, 27, 28, 29, 30, 30, 32, 32, 33, 34, 34, 35, 36, 36, 37, 38, 40, 41, 41, 42, 42, 46, 48, 48, 49, 51, 54, 55, 60)
total_biomass <- c(0.25, 0.65, 0.40, 0.40, 0.65, 0.60, 0.20, 0.60, 0.50, 0.35, 0.70, 0.65, 0.95, 0.60, 0.80, 0.90, 0.70, 1.15, 1.00, 1.70, 1.95, 1.15, 1.70, 1.25, 1.95, 1.20,
2.70, 2.00, 3.70, 2.35, 1.50, 2.50, 5.05, 4.10, 2.85, 2.15, 3.50, 4.80, 5.30, 2.95)
stem_data.df <- data.frame(stem_diameter, total_biomass)
我尝试如下拟合线性模型:
model1 <- lm(total_biomass ~ stem_diameter, data = stem_data.df)
summary(model1)
plot(total_biomass ~ stem_diameter)
lines(stem_diameter, predict(model1))
但是我认为非线性模型更适合于数据。
然后我继续执行此操作:
m <- nls(total_biomass ~ a(stem_diameter^b), stem_data.df, start = list(a = -1.8, b = 0.1)) # power formula: y = a*x^b
summary(m)
plot(total_biomass ~ stem_diameter)
lines(stem_diameter, fitted(m), col = "green")
我在其他地方读过,其中a和b是来自上述线性模型的系数。但是,曲线上的曲线看起来并不理想,导致我认为公式本身存在问题。数据本身似乎很好,因为我已经能够在Excel散点图中绘制一条曲线,该散点图的R2值为0.79,公式y = 0.0009x ^ 2.0598。更改a和b的值对曲线也没有实际影响,因此我不确定问题可能出在哪里。
谁能解释可能是什么问题?
预先感谢
答案 0 :(得分:0)
我建议您研究这篇文章:https://stats.stackexchange.com/a/255265/11849
反正
fit1 <- lm(log(total_biomass) ~ log(stem_diameter), data = stem_data.df)
#you were missing a `*` and had bad starting values
m <- nls(total_biomass ~ a*(stem_diameter^b), stem_data.df,
start = list(a = exp(coef(fit1)[1]), b = coef(fit1)[2])) # power formula: y = a*x^b
summary(m)
plot(total_biomass ~ stem_diameter)
curve(predict(m, newdata = data.frame(stem_diameter = x)), add = TRUE)