# 安装包
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")
通过运行代码得出图形散点图和预测区间、置信区间。