Non-linear Regression
Data description
# uploading the data into the R-workspace
> CW=read.table(file="http://ramanujan.math.trinity.edu/ekwessi/misc/Locoplay.txt",header=T)
# viewing the first lines of the dataset
>head(CW)
# Plot the data to guess the adequate model
>plot(LP[,2],LP[,3],main="An example of Logarithmic regression", xlab="Locomotion", ylab="Play",col="red")
> y=LP$Locomotion # Renaming the variables
> x=LP$Play # Renaming the variables
> logreg=nls(y~a*x^b,stat=list(a=1,b=0.2), data=LP)
> summary(logreg) # Summary of the nonlinear regression
>n=seq(1.400.0.01)
>m=12.0789*n^0.38509
> lines(n,m,type="l",col="blue", lwd=2) #Fitting the model into the data
# Using nonlinear regression to find the regression coefficients a and b
>lreg2=nls(y~L/(1+exp(a+b*x)),start=list(a=5,b=-1,L=1044),data=CW)
>logreg2=lm(log(y)~log(x), data=LP)
#We find a and b with linearization # y=a*x^b->log(y)=log(a)+blog(x)
#In this case, the real value of a will be the exponetial of the value #extimated from logreg2, while b remains the same
>summary(logreg2) # summary of the nonlinear regression
>k=exp(logreg2$coef[1])*n^logreg2$coef[2]
>lines(n,k,type="l",col="red") # Fitting the model into the data
>legend(230,80,c("With linearization","without Linearization"),fill=c("red","blue"))
We compare the two model using the AIC (Akaike's information Criterion)
> AIC(logreg,logreg2) # Comparing the two models using their AIC.
The second model has a the smallest AIC, thus will be better at predicting Play time using Locomotion .