Created
October 11, 2017 15:08
-
-
Save chrissyhroberts/1d2b3632c16b4de296cad062b9e79da5 to your computer and use it in GitHub Desktop.
Polynomial fits : Run a set of polynomial mode (order 1-4) on some x vs y data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Run a set of polynomial models on some x vs y data. | |
#create a data set x and y | |
x<-1:1100 | |
y<-sqrt(x^6+x/x^4)+x/x^x | |
#Define function to run models | |
polyfit<-function(x,order,raw=TRUE) | |
{ | |
fit<-lm(y~poly(x,order,raw=raw)) | |
return (fit)} | |
#Fit first order model | |
fit1 <- polyfit(x = x,order = 1) | |
summary(fit1) | |
#second degree | |
fit2 <- polyfit(x = x,order = 2) | |
summary(fit2) | |
#third degree | |
fit3 <- polyfit(x = x,order = 3) | |
summary(fit3) | |
#fourth degree | |
fit4 <- polyfit(x = x,order = 4) | |
summary(fit4) | |
#simulate some new data | |
xx <- seq(min(x)-10,max(x)+10, length=500) | |
#plot real data | |
plot(x,y,pch=19,log="",cex=0.2) | |
#add lines for data predicted | |
lines(xx, predict(fit1, data.frame(x=xx)), col="red") | |
lines(xx, predict(fit2, data.frame(x=xx)), col="blue") | |
lines(xx, predict(fit3, data.frame(x=xx)), col="green") | |
lines(xx, predict(fit4, data.frame(x=xx)), col="pink") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment