一元线性回归
x = c(18,23,25,35,65,54,34,56,72,19,23,42,18,39,37)
y=c(202,186,187,180,156,169,174,172,153,199,193,174,198,183,178)
plot(x,y)
model=lm(y~x)
summary(model)
coef(model)
resid(model)
plot(model)
predict(model,data.frame(x=50))
sx=sort(x)
plot(x,y); abline(model)
pred = predict(model,data.frame(x=sx),interval="confidence)
conf = predict(model,data.frame(x=sx),interval="prediction")
plot(x,y); abline(model)
lines(sx,conf[,2]); lines(sx,conf[,3])
lines(sx,pred[,2],lty=3); lines(sx,pred[,3],lty=3)
结果展示:
> x = c(18,23,25,35,65,54,34,56,72,19,23,42,18,39,37) > y=c(202,186,187,180,156,169,174,172,153,199,193,174,198,183,178) > plot(x,y) > model=lm(y~x) > summary(model) Call:lm(formula = y ~ x)Residuals: Min 1Q Median 3Q Max -8.9258 -2.5383 0.3879 3.1867 6.6242 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 210.04846 2.86694 73.27 coef(model)(Intercept) x 210.0484584 -0.7977266 > resid(model) > plot(model)
> pred = predict(model,data.frame(x=sx),interval="confidence) > conf = predict(model,data.frame(x=sx),interval="prediction") > plot(x,y); abline(model)> lines(sx,conf[,2]); lines(sx,conf[,3]) > lines(sx,pred[,2],lty=3); lines(sx,pred[,3],lty=3)
>pre1=predict(model,data.frame(x=sx),level=0.9,interval="confidence")>pre1
fit lwr upr1 195.6894 192.5083 198.87052 195.6894 192.5083 198.87053 194.8917 191.8028 197.98054 191.7007 188.9557 194.44585 191.7007 188.9557 194.44586 190.1053 187.5137 192.69697 182.9258 180.7922 185.05938 182.1280 180.0149 184.24119 180.5326 178.4390 182.626210 178.9371 176.8337 181.040511 176.5439 174.3723 178.715512 166.9712 164.0309 169.911613 165.3758 162.2564 168.495214 158.1962 154.1798 162.212715 152.6121 147.8341 157.3902
领取专属 10元无门槛券
私享最新 技术干货