library(ggplot2)
library(tidyr)
onedata=read.csv("D:/Rdata/Windows/data/chap5/simple linear regression.csv")
ggplot(onedata,aes(x=x,y=y))+
theme_bw()+
geom_point(colour="red")+
geom_smooth(method = lm,formula = y~x)
summary(lm(y~x,data = onedata))
lmp3=lm(y~poly(x,3),data = onedata)
summary(lmp3)
##################
polydata=read.csv("D:/Rdata/Windows/data/chap5/polynomial regression.csv")
ggplot(polydata,aes(x=x,y=y))+
geom_point()+theme_bw()
lmp1=lm(y~poly(x,1),data = polydata)
poly1=predict(lmp1,polydata)
polydata$poly1=poly1
#
lmp2=lm(y~poly(x,2),data = polydata)
poly2=predict(lmp2,polydata)
polydata$poly2=poly2
#
lmp3=lm(y~poly(x,3),data = polydata)
poly3=predict(lmp3,polydata)
polydata$poly3=poly3
#
lmp4=lm(y~poly(x,4),data = polydata)
poly4=predict(lmp4,polydata)
polydata$poly4=poly4
#宽型数据转换成长型数据
polydatalong=gather(polydata,key="model",value = "value",
c("poly1","poly2","poly3","poly4"))
ggplot(polydatalong)+theme_bw()+geom_point(aes(x,y))+
geom_line(aes(x=x,y=value,linetype=model,colour=model),size=0.8)+
theme(legend.position = c(0.1,0.8))
R语言-多项式回归分析
最新推荐文章于 2024-07-12 09:05:15 发布