if(Distributiony=="Normal"){ yy <- y aux.formula <- paste("yy~s(x1,",DF3,") + s(x2,",DF4,")") right <- gam(aux.formula) wrong <- glm(yy~x1+x2) sigmar <- sqrt(sum(right$residuals^2)/right$df.resid) sigmaw <- sqrt(sum(wrong$residuals^2)/wrong$df.resid) right <-predict.gam(right,newdata=data.frame(x1=Age,x2=CD4),type="response") wrong <-predict.gam(wrong,newdata=data.frame(x1=Age,x2=CD4),type="response") phi.r <- sigmar^2*alpha + right phi.w <- sigmaw^2*alpha + wrong weights.r <- rep(0,n); weights.r[r==1] <- 1/(1-p0); weights.w <- rep(0,n); weights.w[r==1] <- 1/(1-p0.linear) } if(Distributiony=="Lognormal"){ yy <- log(y) aux.formula <- paste("yy~s(x1,",DF3,") + s(x2,",DF4,")") if(Linear){ right <- gam(aux.formula) wrong <- glm(yy~x1+x2) ## wrogn actually right sigmar <- sqrt(sum(right$residuals^2)/right$df.resid) sigmaw <- sqrt(sum(wrong$residuals^2)/wrong$df.resid) right <-predict.gam(right,newdata=data.frame(x1=Age,x2=CD4),type="response") wrong <-predict.gam(wrong,newdata=data.frame(x1=Age,x2=CD4),type="response") phi.r <- exp(right + (2*alpha+1)*sigmar^2/2) phi.w <- exp(wrong + (2*alpha+1)*sigmaw^2/2) } else{ right <- gam(aux.formula) wrong <- glm(y~x2,family="Gamma") shape <- 1/summary(wrong)$dispersion ###i think dispersion is 1/shape sigmar <- sqrt(sum(right$residuals^2)/right$df.resid) right <-predict.gam(right,newdata=data.frame(x1=Age,x2=CD4),type="response") wrong <-rep(wrong$fitted[1],n) phi.r <- exp(right + (2*alpha+1)*sigmar^2/2) beta <- wrong/shape phi.w <- (shape+alpha)*beta } weights.r <- rep(0,n); weights.r[r==1] <- 1/(1-p0); weights.w <- rep(0,n); weights.w[r==1] <- 1/(1-p0.linear) }