用基本 R 拟合平方根函数
Fit a square root function with basic R
我是 R 的新手。我已经绘制了数据,我只想为其拟合一个不错的模型。就是这样。
我想我可以在这里拟合一个平方根模型(至少到红色数据点)。或者您有其他建议吗?我不知道该怎么做。
我的代码如下所示:
plot(gbl$g_aH_upper~gbl$windspeed,
main="Boundary Layer Conductance",
ylab="Boundary Layer Conductance [m/s]",ylim=lmts, #adj=1,
xlab="wind speed [m/s]",
xaxt='n',
cex.axis=0.8,
pch=19,
las=1,
col="black",type="p",cex=0.7)
axis(side=1,las=1, at=c(seq(from=0,to=2.0,by=0.1), tick=F))
legend('bottomright', legend=c("Upper leaves","Lower leaves")[-50],
lty=1, col=c('black', 'red'), bty='n', cex=1.0, y.intersp = 0.6, x.intersp = 0.4, seg.len = 0.6,lwd=1.5, text.width = 0.8)
par(new = TRUE)
plot(gbl$g_aH_lower~gbl$windspeed, axes = FALSE, xlab = ' ', ylab = ' ', col="red" ,type="p",cex=0.7,las=1, pch=16)
如您所见,线性模型拟合得不是很好。我想我可以做一个更好的。
既然你们都准备好了关于数据行为的理论,那么该理论应该是模型的基础。在这种情况下 y=beta0 + beta1*sqrt(x).
此模型可以配备标准 lm
功能,而不是 glm
功能。
这是一个使用您的近似数据的示例:
#create limited data
x<-c(0, 1.1, 1.4, 1.7)
y<-c(0.06, 0.115, 0.115, 0.125)
plot(y~x)
model<-lm(y~x)
#plot a linear fit
abline(model, col="blue")
print(summary(model))
#model with the square of x
# I() inhibit interpretation see help(I) for more information
modelsr<-lm(y~I(sqrt(x)))
print(summary(modelsr))
#generate the data to the model
xbar<-seq(0, 1.7, 0.03)
ybar<-modelsr$coefficients[1]+sqrt(xbar)*modelsr$coefficients[2]
#plot model
lines(y=ybar, x=xbar, pch=19, col="green")
是的,平方根(绿线)确实比简单线性回归(蓝线)更适合数据:
我是 R 的新手。我已经绘制了数据,我只想为其拟合一个不错的模型。就是这样。
我想我可以在这里拟合一个平方根模型(至少到红色数据点)。或者您有其他建议吗?我不知道该怎么做。
我的代码如下所示:
plot(gbl$g_aH_upper~gbl$windspeed,
main="Boundary Layer Conductance",
ylab="Boundary Layer Conductance [m/s]",ylim=lmts, #adj=1,
xlab="wind speed [m/s]",
xaxt='n',
cex.axis=0.8,
pch=19,
las=1,
col="black",type="p",cex=0.7)
axis(side=1,las=1, at=c(seq(from=0,to=2.0,by=0.1), tick=F))
legend('bottomright', legend=c("Upper leaves","Lower leaves")[-50],
lty=1, col=c('black', 'red'), bty='n', cex=1.0, y.intersp = 0.6, x.intersp = 0.4, seg.len = 0.6,lwd=1.5, text.width = 0.8)
par(new = TRUE)
plot(gbl$g_aH_lower~gbl$windspeed, axes = FALSE, xlab = ' ', ylab = ' ', col="red" ,type="p",cex=0.7,las=1, pch=16)
如您所见,线性模型拟合得不是很好。我想我可以做一个更好的。
既然你们都准备好了关于数据行为的理论,那么该理论应该是模型的基础。在这种情况下 y=beta0 + beta1*sqrt(x).
此模型可以配备标准 lm
功能,而不是 glm
功能。
这是一个使用您的近似数据的示例:
#create limited data
x<-c(0, 1.1, 1.4, 1.7)
y<-c(0.06, 0.115, 0.115, 0.125)
plot(y~x)
model<-lm(y~x)
#plot a linear fit
abline(model, col="blue")
print(summary(model))
#model with the square of x
# I() inhibit interpretation see help(I) for more information
modelsr<-lm(y~I(sqrt(x)))
print(summary(modelsr))
#generate the data to the model
xbar<-seq(0, 1.7, 0.03)
ybar<-modelsr$coefficients[1]+sqrt(xbar)*modelsr$coefficients[2]
#plot model
lines(y=ybar, x=xbar, pch=19, col="green")
是的,平方根(绿线)确实比简单线性回归(蓝线)更适合数据: