Logistic Regression with R
Data description
>Cw<-raed.table(file="http://ramanujan.math.trinity.edu/ekwessi/misc/ColdWar.txt", header=T) #uploading the data into the
R-workspace.
>head(CW) # viewing the first lines of the dataset
>plot(CW,main="An example of Logistic regression", xlab="Number of years after 1959", ylab="Number of ICBMs",col="red") # Plot
the data to guess the adequate model
>y=CW$NumberOfICBM>x=CW$Years # appropriately renaming the variables.
>lreg=lm(log((1056-y)/y)~x, data=CW) # Finding the values of a and b using linearization. L is assumed to be 1056
based on the horizontal asymptote on the scatter plot
>summary(lreg) # Summary of the logistic regression
>t=seq(1,length(x),0.01)
>z=1056/(1+exp(lreg$coef[1]+lreg$coef[2]*t))
>lines(t,z,type="l",col="blue", lwd=2) #Fitting the model into the data
>lreg2=nls(y~L/(1+exp(a+b*x)),start=list(a=5,b=-1,L=1044),data=CW) # Using nonlinear regression to find the regression
coefficients a, b and L.
>summary(lreg2)
> AIC(lreg,lreg2) # Comparing the two models using their AIC.
>u=seq(1,length(x),0.01)
>v=1033.3655/(1+exp(4.39-1.0730*u))
>lines(u,v,type="l",col="red", lwd=2)
>legend(6,200,c("Assuming L=1056","Without assumptions on L"), fill=c("blue","red")) # Overlaying the two models on the same
graph