R语言 多项式回归示例
rm(list=ls())
data <- swiss
data[,1:5] <- scale(data[,1:5])
data1 <- data
for (i in 1:3) {
mdl <- lm(Examination ~ poly(Education, degree=i), data = data)
data1[,6+i] <- predict(mdl,data)
}
# 作图
library(ggplot2)
ggplot(data1)+
geom_point(aes(x=Education,y=Examination))+
geom_line(data=data1, aes(x=Education,y=V7),color="red")+
geom_line(data=data1, aes(x=Education,y=V8),color="blue")+
geom_line(data=data1, aes(x=Education,y=V9),color="green")
|
请发表评论