I'm trying to better understand some of the theory behind fitting models that have a nonlinear link between the response and the predictors.
set.seed(1) #Create a random exponential sample n<-1000 y<-rexp(n=n,rate=.01) y<-y[order(y)] x<-seq(n) df1<-data.frame(cbind(x,y)) plot(x,y)
Now I will attempt 4 different models fits and explain what I expect as a result versus the actual result.
m1<-lm(I(log(y))~x,data=df1) summary(m1) out1<-exp(predict(m1,type='response')) lines(out1)
I expect the m1 model to be the worst fit. This is OLS with the log of the response taken before fitting the model. By taking the log of the response I have a model that, when exponentiated, cannot be negative. Therefore, the assumptions of normally distributed residuals with a constant variance cannot hold. The graph appears to under weight the tails of the distribution significantly.
Next, I will fit the model using a GLM.
m2<-glm(y~x,data=df1,family=gaussian(link='log')) summary(m2) out2<-predict(m2,type='response') lines(out2,col='red')
I expect the m2 model to be of a slightly different fit than m1. This is due to the fact we are now modeling log(y+ϵ)=Xβ rather than modeling log(y)=Xβ+ϵ. I cannot justify if this fit should be better or worse from a theoretical standpoint. R gives an R-squared figure for lm() functions calls but not glm() (rather it gives and AIC score). Looking at the plot it appears that m2 is more poorly matched in the tails of the distribution than m1. The issues with normally distributed residuals should still be the same.
For m3, I relax the residual distribution assumptions by changing to a Gamma family residual distribution. Since an exponential distribution is a Gamma family distribution, I expect this model to very closely match the sampled data.
m3<-glm(y~x,data=df1,family=Gamma(link='log')) summary(m3) out3<-predict(m3,type='response') lines(out3,col='blue')
AIC suggests that m3 is a better fit than m2, but it still does not appear to be a very good fit. In fact, it appears to be a worse estimate in the tails of the distribution than m1.
The m4 model will use nls just for a different approach.
m4<-nls(y~exp(a+b*x),data=df1, start = list(a = 0, b = 0)) summary(m4) out4<-predict(m4,type='response') lines(out4,col='yellow') #for direct comparison t1<-cbind(out1,out2,out3,out4,y)
This appears to very closely match m2. I also expected this to very closely fit the data.
My questions are - how come does m3 not more closely match my data? Is there a way using glm() to more closely fit this model? I realize that if I am randomly sampling then the tails of my sample data will not fit my model exactly, but these appear to be nowhere close. Perhaps letting n approach infinity would all one (or more) of the models to converge, but rerunning the code with n=100,000 actually appears to be worse fitting models (perhaps that's because with 100,000 random samples more outliers were selected and due to how the graph is present an undue amount of focus is given to these outliers?).
I also realize that an exponential distribution is not the same as exponentiating a normal distribution. That is to say, I realize that just because I took the log the of response doesn't mean that I should get a "perfect model" as a result; however, in m3 when I am fitting the models to a Gamma family distribution, I expected to be able to get a very close fit.
Thanks in advance.