当前位置:   article > 正文

一元线性回归-R语言

一元线性回归-R语言

# # 安装包
# install.packages('ggplot2')
# library(ggplot2)
Sys.setlocale(category = 'LC_ALL', locale = 'English_United States.1252')
# Sys.setlocale("LC_ALL","Chinese")
x <- c(18, 20, 22, 24, 26, 28, 30)
y <- c(26.86, 28.35, 28.87,28.75, 29.75, 30, 30.36)
model <- lm(y ~ x)
summary(model)
# 画出散点图
plot(x, y, pch = 19, col = "blue", cex = 2, xlab = "x", ylab = "y")# 绘图代码
 
# 画出拟合直线
plot(model)
# # 画出残差图
# plot(model, which = 1)
# # 画出标准化残差图
# plot(model, which = 2)
# # 画出QQ图
# plot(model, which = 3)
# # 画出方差-拟合值图
# plot(model, which = 4)
# # 画出学生化残差图
# plot(model, which = 5)
# # 画出Cook距离图
# plot(model, which = 6)
# 预测
predict(model, data.frame(x = 40))
# 置信区间
predict(model, data.frame(x = c(40)), interval = "confidence")
# 预测区间
predict(model, data.frame(x = c(40)), interval = "prediction")
 
# install.packages('lifecycle')
#
# options(repos="http://cran.rstudio.com/")
# install.packages("lifecycle", dependencies=TRUE)
# Install release version from CRAN
# install.packages("lifecycle")
#
# # Install development version from GitHub
# pak::pak("r-lib/lifecycle")

通过运行代码得出图形散点图和预测区间、置信区间。

4a1cf40575264c4cabf697a62277a127.jpeg

7575bcbe6036443abe6eb7279b63cd45.jpeg 

a46320720445483ea3fef691637c19ab.jpeg 

65cdede90a1d46e1b7a25ae8b9c39d55.jpeg 

22002c885bd1438caa67a544375f1d41.jpeg 

716a2ff25cea4c08af393c90a18cb38a.jpeg 

96f0deab5f5942368bb771b2f302eff9.jpeg 

77f6c27bbb8c4574bf9e17adff9b25d3.jpeg 

90e299e308424805b048af1ff8156051.jpeg 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/774585
推荐阅读
相关标签
  

闽ICP备14008679号