######################################################### ### R programs for simulation study in Section 5 of the paper ### Defining and Estimating Intervention Effects for Groups That Will ### Develop an Auxiliary Outcome ### by Marshall Joffe, Dylan Small and Chi-yuan Hsu ######################################################### expit=function(x){ exp(x)/(1+exp(x)); } ##################################### # Estimation for a Model that includes a binary covariate # Set covariate X to have a constant effect over all A levels and # principal strata ##################################### # Setting I(A) # Simulation study sims=1; # Number of simulations # Vectors to store estimates from simulation study mean.immune.a1.est.1=rep(0,sims); mean.immune.a0.est.1=rep(0,sims); mean.treatprotected.a1.est.1=rep(0,sims); mean.treatprotected.a0.est.1=rep(0,sims); mean.treatharmful.a1.est.1=rep(0,sims); mean.treatharmful.a0.est.1=rep(0,sims); mean.doomed.a1.est.1=rep(0,sims); mean.doomed.a0.est.1=rep(0,sims); sd.immune.a1.est.1=rep(0,sims); sd.immune.a0.est.1=rep(0,sims); sd.treatprotected.a1.est.1=rep(0,sims); sd.treatprotected.a0.est.1=rep(0,sims); sd.treatharmful.a1.est.1=rep(0,sims); sd.treatharmful.a0.est.1=rep(0,sims); sd.doomed.a1.est.1=rep(0,sims); sd.doomed.a0.est.1=rep(0,sims); prob.immune.est.1=rep(0,sims); prob.treatprotected.est.1=rep(0,sims); prob.treatharmful.est.1=rep(0,sims); prob.doomed.est.1=rep(0,sims); xeffect.est.1=rep(0,sims); psi0.est=rep(0,sims); # Structural nested model coefficients psi1.est=rep(0,sims); betahat.a=rep(0,sims); betahat.mua=rep(0,sims); lldistvec.1=rep(0,sims); no.notconv=0; # Number of times EM algorithm does not converge itt.immune.treatprotected.est.2=rep(0,sims); mean.immune.a0.est.2=rep(0,sims); mean.treatprotected.a0.est.2=rep(0,sims); itt.treatharmful.doomed.est.2=rep(0,sims); mean.treatharmful.a0.est.2=rep(0,sims); mean.doomed.a0.est.2=rep(0,sims); sd.immune.a1.est.2=rep(0,sims); sd.immune.a0.est.2=rep(0,sims); sd.treatprotected.a1.est.2=rep(0,sims); sd.treatprotected.a0.est.2=rep(0,sims); sd.treatharmful.a1.est.2=rep(0,sims); sd.treatharmful.a0.est.2=rep(0,sims); sd.doomed.a1.est.2=rep(0,sims); sd.doomed.a0.est.2=rep(0,sims); xeffect.est.2=rep(0,sims); lldistvec.2=rep(0,sims); k=1; while(k<=sims){ # Simulation from model with a binary covariate # Probabilities of principal strata prob.immune=.25; prob.treatprotected=.4; prob.treatharmful=.05; prob.doomed=.3; # Means and standard deviations of each principal strata under aggressive treatment # for hypertension and under not aggressive treatment for hypertension mean.immune.a1=2; # Mean of immune subjects when aggressively treated for hypertension sd.immune.a1=1; # Variance of immune subjects when aggressively treated mean.immune.a0=1; # Mean of immune subjects when not agressively treated sd.immune.a0=1; # Variance of immune subjects when not aggressively treated mean.treatprotected.a1=2.5; sd.treatprotected.a1=1; mean.treatprotected.a0=1.5; sd.treatprotected.a0=1; mean.treatharmful.a1=1.25; sd.treatharmful.a1=1; mean.treatharmful.a0=.75; sd.treatharmful.a0=1; mean.doomed.a1=1.75; sd.doomed.a1=1; mean.doomed.a0=1.25; sd.doomed.a0=.25; xeffect=.5; # Addition to mean for binary effect # Probabilities of x for different principal strata probx1.immune=.5; probx1.treatprotected=.75; probx1.treatharmful=.25; probx1.doomed=.5; totalprobx=probx1.immune*prob.immune+probx1.treatprotected*prob.treatprotected+probx1.treatharmful*prob.treatharmful+probx1.doomed*prob.doomed; n=500; # total sample size # Assume that half of subjects are randomized into aggressive treatment # of hypertension and half into nonaggressive treatment aggressive.treatment=c(rep(1,n/2),rep(0,n/2)); principalstrata=rmultinom(n,1,c(prob.immune,prob.treatprotected,prob.treatharmful,prob.doomed)); # Dummy variables for principal strata principalstrata.immune=principalstrata[1,]; principalstrata.treatprotected=principalstrata[2,]; principalstrata.treatharmful=principalstrata[3,]; principalstrata.doomed=principalstrata[4,]; # Covariate X x=principalstrata.immune*rbinom(n,1,probx1.immune)+principalstrata.treatprotected*rbinom(n,1,probx1.treatprotected)+principalstrata.treatharmful*rbinom(n,1,probx1.treatharmful)+principalstrata.doomed*rbinom(n,1,probx1.doomed); # ESRD dummy esrd=principalstrata.doomed+principalstrata.treatprotected*(1-aggressive.treatment)+principalstrata.treatharmful*aggressive.treatment; # Mean and standard deviation of outcome mean.outcome=mean.immune.a1*principalstrata.immune*aggressive.treatment+mean.immune.a0*principalstrata.immune*(1-aggressive.treatment)+mean.treatprotected.a1*principalstrata.treatprotected*aggressive.treatment+mean.treatprotected.a0*principalstrata.treatprotected*(1-aggressive.treatment)+mean.treatharmful.a1*principalstrata.treatharmful*aggressive.treatment+mean.treatharmful.a0*principalstrata.treatharmful*(1-aggressive.treatment)+mean.doomed.a1*principalstrata.doomed*aggressive.treatment+mean.doomed.a0*principalstrata.doomed*(1-aggressive.treatment)+xeffect*x; sd.outcome=sd.immune.a1*principalstrata.immune*aggressive.treatment+sd.immune.a0*principalstrata.immune*(1-aggressive.treatment)+sd.treatprotected.a1*principalstrata.treatprotected*aggressive.treatment+sd.treatprotected.a0*principalstrata.treatprotected*(1-aggressive.treatment)+sd.treatharmful.a1*principalstrata.treatharmful*aggressive.treatment+sd.treatharmful.a0*principalstrata.treatharmful*(1-aggressive.treatment)+sd.doomed.a1*principalstrata.doomed*aggressive.treatment+sd.doomed.a0*principalstrata.doomed*(1-aggressive.treatment); # Generate outcomes from normal distribution y=rnorm(n,mean=mean.outcome,sd=sd.outcome); # Generate outcomes from gamma distribution # scalegamma=sd.outcome^2/mean.outcome; # shapegamma=mean.outcome/scalegamma; # y=rgamma(n,shape=shapegamma,scale=scalegamma); # EM algorithm # Select starting values # For now, start at truth prob.immune.curr=prob.immune; prob.treatprotected.curr=prob.treatprotected; prob.treatharmful.curr=prob.treatharmful; prob.doomed.curr=prob.doomed; prob.immune.x1.curr=(prob.immune*probx1.immune)/totalprobx; prob.treatprotected.x1.curr=(prob.treatprotected*probx1.treatprotected)/totalprobx; prob.treatharmful.x1.curr=(prob.treatharmful*probx1.treatharmful)/totalprobx; prob.doomed.x1.curr=(prob.doomed*probx1.doomed)/totalprobx; prob.immune.x0.curr=(prob.immune*(1-probx1.immune))/(1-totalprobx); prob.treatprotected.x0.curr=(prob.treatprotected*(1-probx1.treatprotected))/(1-totalprobx); prob.treatharmful.x0.curr=(prob.treatharmful*(1-probx1.treatharmful))/(1-totalprobx); prob.doomed.x0.curr=(prob.doomed*(1-probx1.doomed))/(1-totalprobx); mean.immune.a1.curr=mean.immune.a1; sd.immune.a1.curr=sd.immune.a1; mean.immune.a0.curr=mean.immune.a0; sd.immune.a0.curr=sd.immune.a0; mean.treatprotected.a1.curr=mean.treatprotected.a1; sd.treatprotected.a1.curr=sd.treatprotected.a1; mean.treatprotected.a0.curr=mean.treatprotected.a0; sd.treatprotected.a0.curr=sd.treatprotected.a0; mean.treatharmful.a1.curr=mean.treatharmful.a1; sd.treatharmful.a1.curr=sd.treatharmful.a1; mean.treatharmful.a0.curr=mean.treatharmful.a0; sd.treatharmful.a0.curr=sd.treatharmful.a0; mean.doomed.a1.curr=mean.doomed.a1; sd.doomed.a1.curr=sd.doomed.a1; mean.doomed.a0.curr=mean.doomed.a0; sd.doomed.a0.curr=sd.doomed.a0; xeffect.curr=xeffect; maxiters=500; # Maximum iterations in EM algorithm iter=1; lldist=1; # Will measure difference in log likelihood between iterations toler=.001; # Tolerance for difference in log likelihood between iterations # for stopping EM algorithm conv=1; while(lldist>toler && iter=.00001 & sd.immune.a0.curr>=.00001 & sd.treatprotected.a1.curr>=.00001 & sd.treatprotected.a0.curr>=.00001 & sd.doomed.a1.curr>=.00001 & sd.doomed.a0.curr>=.00001 & sd.treatharmful.a1.curr>=.00001 & sd.treatharmful.a0.curr>=.00001){ lldist=ll.new-ll.old; iter=iter+1; } } if(is.na(ll.new)==TRUE){ no.notconv=no.notconv+1; conv=0; lldist=0; } } mean.immune.a1.est.1[k]=mean.immune.a1.curr; mean.immune.a0.est.1[k]=mean.immune.a0.curr; mean.treatprotected.a1.est.1[k]=mean.treatprotected.a1.curr; mean.treatprotected.a0.est.1[k]=mean.treatprotected.a0.curr; mean.treatharmful.a1.est.1[k]=mean.treatharmful.a1.curr; mean.treatharmful.a0.est.1[k]=mean.treatharmful.a0.curr; mean.doomed.a1.est.1[k]=mean.doomed.a1.curr; mean.doomed.a0.est.1[k]=mean.doomed.a0.curr; sd.immune.a1.est.1[k]=sd.immune.a1.curr; sd.immune.a0.est.1[k]=sd.immune.a0.curr; sd.treatprotected.a1.est.1[k]=sd.treatprotected.a1.curr; sd.treatprotected.a0.est.1[k]=sd.treatprotected.a0.curr; sd.treatharmful.a1.est.1[k]=sd.treatharmful.a1.curr; sd.treatharmful.a0.est.1[k]=sd.treatharmful.a0.curr; sd.doomed.a1.est.1[k]=sd.doomed.a1.curr; sd.doomed.a0.est.1[k]=sd.doomed.a0.curr; prob.immune.est.1[k]=prob.immune.curr; prob.treatprotected.est.1[k]=prob.treatprotected.curr; prob.treatharmful.est.1[k]=prob.treatharmful.curr; prob.doomed.est.1[k]=prob.doomed.curr; xeffect.est.1[k]=xeffect.curr; lldistvec.1[k]=lldist; # Expected auxiliary stratification estimates ax=aggressive.treatment*x; auxreg1=glm(esrd~x+aggressive.treatment+ax,family=binomial); # muhatax=predict(auxreg1,type="response"); muhatax=predict.glm(auxreg1,type="response"); a.times.muhatax=aggressive.treatment*muhatax; auxreg2=lm(y~muhatax+aggressive.treatment+a.times.muhatax); betahat.a[k]=coef(auxreg2)[3]; betahat.mua[k]=coef(auxreg2)[4]; # Structural nested model estimates # Use Newton's Method # Start at true values psi1.curr=mean.doomed.a1-mean.doomed.a0; psi0.curr=mean.treatprotected.a1-mean.treatprotected.a0; # Estimate "mediating score" tempreg=glm(esrd~x,family=binomial,subset=(aggressive.treatment==1)); mediatingscore=expit(coef(tempreg)[1]+coef(tempreg)[2]*x); maxiters=500; # Maximum iterations in Newton-Raphson algorithm iter=1; dist=1; toler=.001; while(itertoler){ psiold=matrix(c(psi0.curr,psi1.curr),ncol=1); y0psi=y-psi0.curr*aggressive.treatment*(1-esrd)-psi1.curr*aggressive.treatment*esrd; tempreg=lm(y0psi~x); tempbetahat0=coef(tempreg)[1]; tempbetahat1=coef(tempreg)[2]; g1=sum((aggressive.treatment-.5)*(y0psi-tempbetahat0-tempbetahat1*x)*(1-mediatingscore)); g2=sum((aggressive.treatment-.5)*(y0psi-tempbetahat0-tempbetahat1*x)*mediatingscore); Jmat=matrix(rep(0,4),ncol=2); Jmat[1,1]=sum(-(aggressive.treatment-.5)*aggressive.treatment*(1-esrd)*(1-mediatingscore)); Jmat[1,2]=sum(-(aggressive.treatment-.5)*aggressive.treatment*esrd*(1-mediatingscore)); Jmat[2,1]=sum(-(aggressive.treatment-.5)*aggressive.treatment*(1-esrd)*mediatingscore); Jmat[2,2]=sum(-(aggressive.treatment-.5)*aggressive.treatment*esrd*mediatingscore); psinew=psiold-solve(Jmat)%*%matrix(c(g1,g2),ncol=1); dist=(sum((psinew-psiold)^2))^.5; psi0.curr=psinew[1]; psi1.curr=psinew[2]; iter=iter+1; } psi0.est[k]=psi0.curr; psi1.est[k]=psi1.curr; # Estimates that assume ITT(immune)=ITT(treatment protected) # ITT(treatment harmful)=ITT(doomed) # EM algorithm # Select starting values # For now, start at truth prob.immune.curr=prob.immune; prob.treatprotected.curr=prob.treatprotected; prob.treatharmful.curr=prob.treatharmful; prob.doomed.curr=prob.doomed; prob.immune.x1.curr=(prob.immune*probx1.immune)/totalprobx; prob.treatprotected.x1.curr=(prob.treatprotected*probx1.treatprotected)/totalprobx; prob.treatharmful.x1.curr=(prob.treatharmful*probx1.treatharmful)/totalprobx; prob.doomed.x1.curr=(prob.doomed*probx1.doomed)/totalprobx; prob.immune.x0.curr=(prob.immune*(1-probx1.immune))/(1-totalprobx); prob.treatprotected.x0.curr=(prob.treatprotected*(1-probx1.treatprotected))/(1-totalprobx); prob.treatharmful.x0.curr=(prob.treatharmful*(1-probx1.treatharmful))/(1-totalprobx); prob.doomed.x0.curr=(prob.doomed*(1-probx1.doomed))/(1-totalprobx); itt.immune.treatprotected.curr=mean.immune.a1-mean.immune.a0; sd.immune.a1.curr=sd.immune.a1; mean.immune.a0.curr=mean.immune.a0; sd.immune.a0.curr=sd.immune.a0; sd.treatprotected.a1.curr=sd.treatprotected.a1; mean.treatprotected.a0.curr=mean.treatprotected.a0; sd.treatprotected.a0.curr=sd.treatprotected.a0; itt.treatharmful.doomed.curr=mean.treatharmful.a1-mean.treatharmful.a0; sd.treatharmful.a1.curr=sd.treatharmful.a1; mean.treatharmful.a0.curr=mean.treatharmful.a0; sd.treatharmful.a0.curr=sd.treatharmful.a0; sd.doomed.a1.curr=sd.doomed.a1; mean.doomed.a0.curr=mean.doomed.a0; sd.doomed.a0.curr=sd.doomed.a0; xeffect.curr=xeffect; maxiters=500; # Maximum iterations in EM algorithm iter=1; lldist=1; # Will measure difference in log likelihood between iterations toler=.001; # Tolerance for difference in log likelihood between iterations # for stopping EM algorithm conv=1; while(lldist>toler && iter=.00001 & sd.immune.a0.curr>=.00001 & sd.treatprotected.a1.curr>=.00001 & sd.treatprotected.a0.curr>=.00001 & sd.doomed.a1.curr>=.00001 & sd.doomed.a0.curr>=.00001 & sd.treatharmful.a1.curr>=.00001 & sd.treatharmful.a0.curr>=.00001){ lldist=ll.new-ll.old; iter=iter+1; } } if(is.na(ll.new)==TRUE){ no.notconv=no.notconv+1; conv=0; lldist=0; } } if(conv==1){ itt.immune.treatprotected.est.2[k]=itt.immune.treatprotected.curr; mean.immune.a0.est.2[k]=mean.immune.a0.curr; mean.treatprotected.a0.est.2[k]=mean.treatprotected.a0.curr; itt.treatharmful.doomed.est.2[k]=itt.treatharmful.doomed.curr; mean.treatharmful.a0.est.2[k]=mean.treatharmful.a0.curr; mean.doomed.a0.est.2[k]=mean.doomed.a0.curr; sd.immune.a1.est.2[k]=sd.immune.a1.curr; sd.immune.a0.est.2[k]=sd.immune.a0.curr; sd.treatprotected.a1.est.2[k]=sd.treatprotected.a1.curr; sd.treatprotected.a0.est.2[k]=sd.treatprotected.a0.curr; sd.treatharmful.a1.est.2[k]=sd.treatharmful.a1.curr; sd.treatharmful.a0.est.2[k]=sd.treatharmful.a0.curr; sd.doomed.a1.est.2[k]=sd.doomed.a1.curr; sd.doomed.a0.est.2[k]=sd.doomed.a0.curr; xeffect.est.2[k]=xeffect.curr; lldistvec.2[k]=lldist; k=k+1; } } conv=as.logical((1-is.na(mean.immune.a1.est.1-mean.immune.a0.est.1))*(1-is.na(mean.treatprotected.a1.est.1-mean.treatprotected.a0.est.1))*(1-is.na(mean.treatharmful.a1.est.1-mean.treatharmful.a0.est.1))*(1-is.na(mean.doomed.a1.est.1-mean.doomed.a0.est.1))*(1-is.na(psi0.est))*(1-is.na(psi1.est))*(1-is.na(itt.immune.treatprotected.est.2))*(1-is.na(itt.treatharmful.doomed.est.2))); sum(conv) mean(mean.immune.a1.est.1[conv]-mean.immune.a0.est.1[conv]) sd(mean.immune.a1.est.1[conv]-mean.immune.a0.est.1[conv]) mean(mean.treatprotected.a1.est.1[conv]-mean.treatprotected.a0.est.1[conv]) sd(mean.treatprotected.a1.est.1[conv]-mean.treatprotected.a0.est.1[conv]) mean(mean.treatharmful.a1.est.1[conv]-mean.treatharmful.a0.est.1[conv]) sd(mean.treatharmful.a1.est.1[conv]-mean.treatharmful.a0.est.1[conv]) mean(mean.doomed.a1.est.1[conv]-mean.doomed.a0.est.1[conv]) sd(mean.doomed.a1.est.1[conv]-mean.doomed.a0.est.1[conv]) mean(psi0.est[conv]) sd(psi0.est[conv]) mean(psi1.est[conv]) sd(psi1.est[conv]) mean(betahat.a[conv]+betahat.mua[conv]) # sd(betahat.a+betahat.mua) mean(itt.immune.treatprotected.est.2[conv]) sd(itt.immune.treatprotected.est.2[conv]) mean(itt.treatharmful.doomed.est.2[conv]) sd(itt.treatharmful.doomed.est.2[conv]) # Setting I(B) # Simulation study sims=1; # Number of simulations # Vectors to store estimates from simulation study mean.immune.a1.est.1=rep(0,sims); mean.immune.a0.est.1=rep(0,sims); mean.treatprotected.a1.est.1=rep(0,sims); mean.treatprotected.a0.est.1=rep(0,sims); mean.treatharmful.a1.est.1=rep(0,sims); mean.treatharmful.a0.est.1=rep(0,sims); mean.doomed.a1.est.1=rep(0,sims); mean.doomed.a0.est.1=rep(0,sims); sd.immune.a1.est.1=rep(0,sims); sd.immune.a0.est.1=rep(0,sims); sd.treatprotected.a1.est.1=rep(0,sims); sd.treatprotected.a0.est.1=rep(0,sims); sd.treatharmful.a1.est.1=rep(0,sims); sd.treatharmful.a0.est.1=rep(0,sims); sd.doomed.a1.est.1=rep(0,sims); sd.doomed.a0.est.1=rep(0,sims); prob.immune.est.1=rep(0,sims); prob.treatprotected.est.1=rep(0,sims); prob.treatharmful.est.1=rep(0,sims); prob.doomed.est.1=rep(0,sims); xeffect.est.1=rep(0,sims); psi0.est=rep(0,sims); # Structural nested model coefficients psi1.est=rep(0,sims); betahat.a=rep(0,sims); betahat.mua=rep(0,sims); lldistvec.1=rep(0,sims); no.notconv=0; # Number of times EM algorithm does not converge itt.immune.treatprotected.est.2=rep(0,sims); mean.immune.a0.est.2=rep(0,sims); mean.treatprotected.a0.est.2=rep(0,sims); itt.treatharmful.doomed.est.2=rep(0,sims); mean.treatharmful.a0.est.2=rep(0,sims); mean.doomed.a0.est.2=rep(0,sims); sd.immune.a1.est.2=rep(0,sims); sd.immune.a0.est.2=rep(0,sims); sd.treatprotected.a1.est.2=rep(0,sims); sd.treatprotected.a0.est.2=rep(0,sims); sd.treatharmful.a1.est.2=rep(0,sims); sd.treatharmful.a0.est.2=rep(0,sims); sd.doomed.a1.est.2=rep(0,sims); sd.doomed.a0.est.2=rep(0,sims); xeffect.est.2=rep(0,sims); lldistvec.2=rep(0,sims); k=1; while(k<=sims){ # Simulation from model with a binary covariate # Probabilities of principal strata prob.immune=.25; prob.treatprotected=.4; prob.treatharmful=.05; prob.doomed=.3; # Means and standard deviations of each principal strata under aggressive treatment # for hypertension and under not aggressive treatment for hypertension mean.immune.a1=2; # Mean of immune subjects when aggressively treated for hypertension sd.immune.a1=1; # Variance of immune subjects when aggressively treated mean.immune.a0=1; # Mean of immune subjects when not agressively treated sd.immune.a0=1; # Variance of immune subjects when not aggressively treated mean.treatprotected.a1=2.5; sd.treatprotected.a1=1; mean.treatprotected.a0=1.5; sd.treatprotected.a0=1; mean.treatharmful.a1=1.25; sd.treatharmful.a1=1; mean.treatharmful.a0=.75; sd.treatharmful.a0=1; mean.doomed.a1=1.75; sd.doomed.a1=1; mean.doomed.a0=1.25; sd.doomed.a0=.25; xeffect=.5; # Addition to mean for binary effect # Probabilities of x for different principal strata probx1.immune=.5; probx1.treatprotected=.75; probx1.treatharmful=.25; probx1.doomed=.5; totalprobx=probx1.immune*prob.immune+probx1.treatprotected*prob.treatprotected+probx1.treatharmful*prob.treatharmful+probx1.doomed*prob.doomed; n=500; # total sample size # Assume that half of subjects are randomized into aggressive treatment # of hypertension and half into nonaggressive treatment aggressive.treatment=c(rep(1,n/2),rep(0,n/2)); principalstrata=rmultinom(n,1,c(prob.immune,prob.treatprotected,prob.treatharmful,prob.doomed)); # Dummy variables for principal strata principalstrata.immune=principalstrata[1,]; principalstrata.treatprotected=principalstrata[2,]; principalstrata.treatharmful=principalstrata[3,]; principalstrata.doomed=principalstrata[4,]; # Covariate X x=principalstrata.immune*rbinom(n,1,probx1.immune)+principalstrata.treatprotected*rbinom(n,1,probx1.treatprotected)+principalstrata.treatharmful*rbinom(n,1,probx1.treatharmful)+principalstrata.doomed*rbinom(n,1,probx1.doomed); # ESRD dummy esrd=principalstrata.doomed+principalstrata.treatprotected*(1-aggressive.treatment)+principalstrata.treatharmful*aggressive.treatment; # Mean and standard deviation of outcome mean.outcome=mean.immune.a1*principalstrata.immune*aggressive.treatment+mean.immune.a0*principalstrata.immune*(1-aggressive.treatment)+mean.treatprotected.a1*principalstrata.treatprotected*aggressive.treatment+mean.treatprotected.a0*principalstrata.treatprotected*(1-aggressive.treatment)+mean.treatharmful.a1*principalstrata.treatharmful*aggressive.treatment+mean.treatharmful.a0*principalstrata.treatharmful*(1-aggressive.treatment)+mean.doomed.a1*principalstrata.doomed*aggressive.treatment+mean.doomed.a0*principalstrata.doomed*(1-aggressive.treatment)+xeffect*x; sd.outcome=sd.immune.a1*principalstrata.immune*aggressive.treatment+sd.immune.a0*principalstrata.immune*(1-aggressive.treatment)+sd.treatprotected.a1*principalstrata.treatprotected*aggressive.treatment+sd.treatprotected.a0*principalstrata.treatprotected*(1-aggressive.treatment)+sd.treatharmful.a1*principalstrata.treatharmful*aggressive.treatment+sd.treatharmful.a0*principalstrata.treatharmful*(1-aggressive.treatment)+sd.doomed.a1*principalstrata.doomed*aggressive.treatment+sd.doomed.a0*principalstrata.doomed*(1-aggressive.treatment); # Generate outcomes from normal distribution # y=rnorm(n,mean=mean.outcome,sd=sd.outcome); # Generate outcomes from gamma distribution scalegamma=sd.outcome^2/mean.outcome; shapegamma=mean.outcome/scalegamma; y=rgamma(n,shape=shapegamma,scale=scalegamma); # EM algorithm # Select starting values # For now, start at truth prob.immune.curr=prob.immune; prob.treatprotected.curr=prob.treatprotected; prob.treatharmful.curr=prob.treatharmful; prob.doomed.curr=prob.doomed; prob.immune.x1.curr=(prob.immune*probx1.immune)/totalprobx; prob.treatprotected.x1.curr=(prob.treatprotected*probx1.treatprotected)/totalprobx; prob.treatharmful.x1.curr=(prob.treatharmful*probx1.treatharmful)/totalprobx; prob.doomed.x1.curr=(prob.doomed*probx1.doomed)/totalprobx; prob.immune.x0.curr=(prob.immune*(1-probx1.immune))/(1-totalprobx); prob.treatprotected.x0.curr=(prob.treatprotected*(1-probx1.treatprotected))/(1-totalprobx); prob.treatharmful.x0.curr=(prob.treatharmful*(1-probx1.treatharmful))/(1-totalprobx); prob.doomed.x0.curr=(prob.doomed*(1-probx1.doomed))/(1-totalprobx); mean.immune.a1.curr=mean.immune.a1; sd.immune.a1.curr=sd.immune.a1; mean.immune.a0.curr=mean.immune.a0; sd.immune.a0.curr=sd.immune.a0; mean.treatprotected.a1.curr=mean.treatprotected.a1; sd.treatprotected.a1.curr=sd.treatprotected.a1; mean.treatprotected.a0.curr=mean.treatprotected.a0; sd.treatprotected.a0.curr=sd.treatprotected.a0; mean.treatharmful.a1.curr=mean.treatharmful.a1; sd.treatharmful.a1.curr=sd.treatharmful.a1; mean.treatharmful.a0.curr=mean.treatharmful.a0; sd.treatharmful.a0.curr=sd.treatharmful.a0; mean.doomed.a1.curr=mean.doomed.a1; sd.doomed.a1.curr=sd.doomed.a1; mean.doomed.a0.curr=mean.doomed.a0; sd.doomed.a0.curr=sd.doomed.a0; xeffect.curr=xeffect; maxiters=500; # Maximum iterations in EM algorithm iter=1; lldist=1; # Will measure difference in log likelihood between iterations toler=.001; # Tolerance for difference in log likelihood between iterations # for stopping EM algorithm conv=1; while(lldist>toler && iter=.00001 & sd.immune.a0.curr>=.00001 & sd.treatprotected.a1.curr>=.00001 & sd.treatprotected.a0.curr>=.00001 & sd.doomed.a1.curr>=.00001 & sd.doomed.a0.curr>=.00001 & sd.treatharmful.a1.curr>=.00001 & sd.treatharmful.a0.curr>=.00001){ lldist=ll.new-ll.old; iter=iter+1; } } if(is.na(ll.new)==TRUE){ no.notconv=no.notconv+1; conv=0; lldist=0; } } mean.immune.a1.est.1[k]=mean.immune.a1.curr; mean.immune.a0.est.1[k]=mean.immune.a0.curr; mean.treatprotected.a1.est.1[k]=mean.treatprotected.a1.curr; mean.treatprotected.a0.est.1[k]=mean.treatprotected.a0.curr; mean.treatharmful.a1.est.1[k]=mean.treatharmful.a1.curr; mean.treatharmful.a0.est.1[k]=mean.treatharmful.a0.curr; mean.doomed.a1.est.1[k]=mean.doomed.a1.curr; mean.doomed.a0.est.1[k]=mean.doomed.a0.curr; sd.immune.a1.est.1[k]=sd.immune.a1.curr; sd.immune.a0.est.1[k]=sd.immune.a0.curr; sd.treatprotected.a1.est.1[k]=sd.treatprotected.a1.curr; sd.treatprotected.a0.est.1[k]=sd.treatprotected.a0.curr; sd.treatharmful.a1.est.1[k]=sd.treatharmful.a1.curr; sd.treatharmful.a0.est.1[k]=sd.treatharmful.a0.curr; sd.doomed.a1.est.1[k]=sd.doomed.a1.curr; sd.doomed.a0.est.1[k]=sd.doomed.a0.curr; prob.immune.est.1[k]=prob.immune.curr; prob.treatprotected.est.1[k]=prob.treatprotected.curr; prob.treatharmful.est.1[k]=prob.treatharmful.curr; prob.doomed.est.1[k]=prob.doomed.curr; xeffect.est.1[k]=xeffect.curr; lldistvec.1[k]=lldist; # Expected auxiliary stratification estimates ax=aggressive.treatment*x; auxreg1=glm(esrd~x+aggressive.treatment+ax,family=binomial); muhatax=predict.glm(auxreg1,type="response"); a.times.muhatax=aggressive.treatment*muhatax; auxreg2=lm(y~muhatax+aggressive.treatment+a.times.muhatax); betahat.a[k]=coef(auxreg2)[3]; betahat.mua[k]=coef(auxreg2)[4]; # Structural nested model estimates # Use Newton's Method # Start at true values psi1.curr=mean.doomed.a1-mean.doomed.a0; psi0.curr=mean.treatprotected.a1-mean.treatprotected.a0; # Estimate "mediating score" tempreg=glm(esrd~x,family=binomial,subset=(aggressive.treatment==1)); mediatingscore=expit(coef(tempreg)[1]+coef(tempreg)[2]*x); maxiters=500; # Maximum iterations in Newton-Raphson algorithm iter=1; dist=1; toler=.001; while(itertoler){ psiold=matrix(c(psi0.curr,psi1.curr),ncol=1); y0psi=y-psi0.curr*aggressive.treatment*(1-esrd)-psi1.curr*aggressive.treatment*esrd; tempreg=lm(y0psi~x); tempbetahat0=coef(tempreg)[1]; tempbetahat1=coef(tempreg)[2]; g1=sum((aggressive.treatment-.5)*(y0psi-tempbetahat0-tempbetahat1*x)*(1-mediatingscore)); g2=sum((aggressive.treatment-.5)*(y0psi-tempbetahat0-tempbetahat1*x)*mediatingscore); Jmat=matrix(rep(0,4),ncol=2); Jmat[1,1]=sum(-(aggressive.treatment-.5)*aggressive.treatment*(1-esrd)*(1-mediatingscore)); Jmat[1,2]=sum(-(aggressive.treatment-.5)*aggressive.treatment*esrd*(1-mediatingscore)); Jmat[2,1]=sum(-(aggressive.treatment-.5)*aggressive.treatment*(1-esrd)*mediatingscore); Jmat[2,2]=sum(-(aggressive.treatment-.5)*aggressive.treatment*esrd*mediatingscore); psinew=psiold-solve(Jmat)%*%matrix(c(g1,g2),ncol=1); dist=(sum((psinew-psiold)^2))^.5; psi0.curr=psinew[1]; psi1.curr=psinew[2]; iter=iter+1; } psi0.est[k]=psi0.curr; psi1.est[k]=psi1.curr; # Estimates that assume ITT(immune)=ITT(treatment protected) # ITT(treatment harmful)=ITT(doomed) # EM algorithm # Select starting values # For now, start at truth prob.immune.curr=prob.immune; prob.treatprotected.curr=prob.treatprotected; prob.treatharmful.curr=prob.treatharmful; prob.doomed.curr=prob.doomed; prob.immune.x1.curr=(prob.immune*probx1.immune)/totalprobx; prob.treatprotected.x1.curr=(prob.treatprotected*probx1.treatprotected)/totalprobx; prob.treatharmful.x1.curr=(prob.treatharmful*probx1.treatharmful)/totalprobx; prob.doomed.x1.curr=(prob.doomed*probx1.doomed)/totalprobx; prob.immune.x0.curr=(prob.immune*(1-probx1.immune))/(1-totalprobx); prob.treatprotected.x0.curr=(prob.treatprotected*(1-probx1.treatprotected))/(1-totalprobx); prob.treatharmful.x0.curr=(prob.treatharmful*(1-probx1.treatharmful))/(1-totalprobx); prob.doomed.x0.curr=(prob.doomed*(1-probx1.doomed))/(1-totalprobx); itt.immune.treatprotected.curr=mean.immune.a1-mean.immune.a0; sd.immune.a1.curr=sd.immune.a1; mean.immune.a0.curr=mean.immune.a0; sd.immune.a0.curr=sd.immune.a0; sd.treatprotected.a1.curr=sd.treatprotected.a1; mean.treatprotected.a0.curr=mean.treatprotected.a0; sd.treatprotected.a0.curr=sd.treatprotected.a0; itt.treatharmful.doomed.curr=mean.treatharmful.a1-mean.treatharmful.a0; sd.treatharmful.a1.curr=sd.treatharmful.a1; mean.treatharmful.a0.curr=mean.treatharmful.a0; sd.treatharmful.a0.curr=sd.treatharmful.a0; sd.doomed.a1.curr=sd.doomed.a1; mean.doomed.a0.curr=mean.doomed.a0; sd.doomed.a0.curr=sd.doomed.a0; xeffect.curr=xeffect; maxiters=500; # Maximum iterations in EM algorithm iter=1; lldist=1; # Will measure difference in log likelihood between iterations toler=.001; # Tolerance for difference in log likelihood between iterations # for stopping EM algorithm conv=1; while(lldist>toler && iter=.00001 & sd.immune.a0.curr>=.00001 & sd.treatprotected.a1.curr>=.00001 & sd.treatprotected.a0.curr>=.00001 & sd.doomed.a1.curr>=.00001 & sd.doomed.a0.curr>=.00001 & sd.treatharmful.a1.curr>=.00001 & sd.treatharmful.a0.curr>=.00001){ lldist=ll.new-ll.old; iter=iter+1; } } if(is.na(ll.new)==TRUE){ no.notconv=no.notconv+1; conv=0; lldist=0; } } if(conv==1){ itt.immune.treatprotected.est.2[k]=itt.immune.treatprotected.curr; mean.immune.a0.est.2[k]=mean.immune.a0.curr; mean.treatprotected.a0.est.2[k]=mean.treatprotected.a0.curr; itt.treatharmful.doomed.est.2[k]=itt.treatharmful.doomed.curr; mean.treatharmful.a0.est.2[k]=mean.treatharmful.a0.curr; mean.doomed.a0.est.2[k]=mean.doomed.a0.curr; sd.immune.a1.est.2[k]=sd.immune.a1.curr; sd.immune.a0.est.2[k]=sd.immune.a0.curr; sd.treatprotected.a1.est.2[k]=sd.treatprotected.a1.curr; sd.treatprotected.a0.est.2[k]=sd.treatprotected.a0.curr; sd.treatharmful.a1.est.2[k]=sd.treatharmful.a1.curr; sd.treatharmful.a0.est.2[k]=sd.treatharmful.a0.curr; sd.doomed.a1.est.2[k]=sd.doomed.a1.curr; sd.doomed.a0.est.2[k]=sd.doomed.a0.curr; xeffect.est.2[k]=xeffect.curr; lldistvec.2[k]=lldist; k=k+1; } } conv=as.logical((1-is.na(mean.immune.a1.est.1-mean.immune.a0.est.1))*(1-is.na(mean.treatprotected.a1.est.1-mean.treatprotected.a0.est.1))*(1-is.na(mean.treatharmful.a1.est.1-mean.treatharmful.a0.est.1))*(1-is.na(mean.doomed.a1.est.1-mean.treatharmful.a0.est.1))*(1-is.na(psi0.est))*(1-is.na(psi1.est))*(1-is.na(itt.immune.treatprotected.est.2))*(1-is.na(itt.treatharmful.doomed.est.2))); sum(conv) mean(mean.immune.a1.est.1[conv]-mean.immune.a0.est.1[conv]) sd(mean.immune.a1.est.1[conv]-mean.immune.a0.est.1[conv]) mean(mean.treatprotected.a1.est.1[conv]-mean.treatprotected.a0.est.1[conv]) sd(mean.treatprotected.a1.est.1[conv]-mean.treatprotected.a0.est.1[conv]) mean(mean.treatharmful.a1.est.1[conv]-mean.treatharmful.a0.est.1[conv]) sd(mean.treatharmful.a1.est.1[conv]-mean.treatharmful.a0.est.1[conv]) mean(mean.doomed.a1.est.1[conv]-mean.doomed.a0.est.1[conv]) sd(mean.doomed.a1.est.1[conv]-mean.doomed.a0.est.1[conv]) mean(psi0.est[conv]) sd(psi0.est[conv]) mean(psi1.est[conv]) sd(psi1.est[conv]) mean(betahat.a[conv]+betahat.mua[conv]) # sd(betahat.a+betahat.mua) mean(itt.immune.treatprotected.est.2[conv]) sd(itt.immune.treatprotected.est.2[conv]) mean(itt.treatharmful.doomed.est.2[conv]) sd(itt.treatharmful.doomed.est.2[conv]) # Setting II(A) # Simulation study sims=1; # Number of simulations # Vectors to store estimates from simulation study mean.immune.a1.est.1=rep(0,sims); mean.immune.a0.est.1=rep(0,sims); mean.treatprotected.a1.est.1=rep(0,sims); mean.treatprotected.a0.est.1=rep(0,sims); mean.treatharmful.a1.est.1=rep(0,sims); mean.treatharmful.a0.est.1=rep(0,sims); mean.doomed.a1.est.1=rep(0,sims); mean.doomed.a0.est.1=rep(0,sims); sd.immune.a1.est.1=rep(0,sims); sd.immune.a0.est.1=rep(0,sims); sd.treatprotected.a1.est.1=rep(0,sims); sd.treatprotected.a0.est.1=rep(0,sims); sd.treatharmful.a1.est.1=rep(0,sims); sd.treatharmful.a0.est.1=rep(0,sims); sd.doomed.a1.est.1=rep(0,sims); sd.doomed.a0.est.1=rep(0,sims); prob.immune.est.1=rep(0,sims); prob.treatprotected.est.1=rep(0,sims); prob.treatharmful.est.1=rep(0,sims); prob.doomed.est.1=rep(0,sims); xeffect.est.1=rep(0,sims); psi0.est=rep(0,sims); # Structural nested model coefficients psi1.est=rep(0,sims); betahat.a=rep(0,sims); betahat.mua=rep(0,sims); lldistvec.1=rep(0,sims); no.notconv=0; # Number of times EM algorithm does not converge itt.immune.treatprotected.est.2=rep(0,sims); mean.immune.a0.est.2=rep(0,sims); mean.treatprotected.a0.est.2=rep(0,sims); itt.treatharmful.doomed.est.2=rep(0,sims); mean.treatharmful.a0.est.2=rep(0,sims); mean.doomed.a0.est.2=rep(0,sims); sd.immune.a1.est.2=rep(0,sims); sd.immune.a0.est.2=rep(0,sims); sd.treatprotected.a1.est.2=rep(0,sims); sd.treatprotected.a0.est.2=rep(0,sims); sd.treatharmful.a1.est.2=rep(0,sims); sd.treatharmful.a0.est.2=rep(0,sims); sd.doomed.a1.est.2=rep(0,sims); sd.doomed.a0.est.2=rep(0,sims); xeffect.est.2=rep(0,sims); lldistvec.2=rep(0,sims); k=1; while(k<=sims){ # Simulation from model with a binary covariate # Probabilities of principal strata prob.immune=.25; prob.treatprotected=.4; prob.treatharmful=.05; prob.doomed=.3; # Means and standard deviations of each principal strata under aggressive treatment # for hypertension and under not aggressive treatment for hypertension mean.immune.a1=2; # Mean of immune subjects when aggressively treated for hypertension sd.immune.a1=1; # Variance of immune subjects when aggressively treated mean.immune.a0=1; # Mean of immune subjects when not agressively treated sd.immune.a0=1; # Variance of immune subjects when not aggressively treated mean.treatprotected.a1=2.5; sd.treatprotected.a1=1; mean.treatprotected.a0=1; sd.treatprotected.a0=1; mean.treatharmful.a1=1.25; sd.treatharmful.a1=1; mean.treatharmful.a0=.75; sd.treatharmful.a0=1; mean.doomed.a1=2.25; sd.doomed.a1=1; mean.doomed.a0=1.25; sd.doomed.a0=.25; xeffect=.5; # Addition to mean for binary effect # Probabilities of x for different principal strata probx1.immune=.5; probx1.treatprotected=.75; probx1.treatharmful=.25; probx1.doomed=.5; totalprobx=probx1.immune*prob.immune+probx1.treatprotected*prob.treatprotected+probx1.treatharmful*prob.treatharmful+probx1.doomed*prob.doomed; n=500; # total sample size # Assume that half of subjects are randomized into aggressive treatment # of hypertension and half into nonaggressive treatment aggressive.treatment=c(rep(1,n/2),rep(0,n/2)); principalstrata=rmultinom(n,1,c(prob.immune,prob.treatprotected,prob.treatharmful,prob.doomed)); # Dummy variables for principal strata principalstrata.immune=principalstrata[1,]; principalstrata.treatprotected=principalstrata[2,]; principalstrata.treatharmful=principalstrata[3,]; principalstrata.doomed=principalstrata[4,]; # Covariate X x=principalstrata.immune*rbinom(n,1,probx1.immune)+principalstrata.treatprotected*rbinom(n,1,probx1.treatprotected)+principalstrata.treatharmful*rbinom(n,1,probx1.treatharmful)+principalstrata.doomed*rbinom(n,1,probx1.doomed); # ESRD dummy esrd=principalstrata.doomed+principalstrata.treatprotected*(1-aggressive.treatment)+principalstrata.treatharmful*aggressive.treatment; # Mean and standard deviation of outcome mean.outcome=mean.immune.a1*principalstrata.immune*aggressive.treatment+mean.immune.a0*principalstrata.immune*(1-aggressive.treatment)+mean.treatprotected.a1*principalstrata.treatprotected*aggressive.treatment+mean.treatprotected.a0*principalstrata.treatprotected*(1-aggressive.treatment)+mean.treatharmful.a1*principalstrata.treatharmful*aggressive.treatment+mean.treatharmful.a0*principalstrata.treatharmful*(1-aggressive.treatment)+mean.doomed.a1*principalstrata.doomed*aggressive.treatment+mean.doomed.a0*principalstrata.doomed*(1-aggressive.treatment)+xeffect*x; sd.outcome=sd.immune.a1*principalstrata.immune*aggressive.treatment+sd.immune.a0*principalstrata.immune*(1-aggressive.treatment)+sd.treatprotected.a1*principalstrata.treatprotected*aggressive.treatment+sd.treatprotected.a0*principalstrata.treatprotected*(1-aggressive.treatment)+sd.treatharmful.a1*principalstrata.treatharmful*aggressive.treatment+sd.treatharmful.a0*principalstrata.treatharmful*(1-aggressive.treatment)+sd.doomed.a1*principalstrata.doomed*aggressive.treatment+sd.doomed.a0*principalstrata.doomed*(1-aggressive.treatment); # Generate outcomes from normal distribution y=rnorm(n,mean=mean.outcome,sd=sd.outcome); # Generate outcomes from gamma distribution # scalegamma=sd.outcome^2/mean.outcome; # shapegamma=mean.outcome/scalegamma; # y=rgamma(n,shape=shapegamma,scale=scalegamma); # EM algorithm # Select starting values # For now, start at truth prob.immune.curr=prob.immune; prob.treatprotected.curr=prob.treatprotected; prob.treatharmful.curr=prob.treatharmful; prob.doomed.curr=prob.doomed; prob.immune.x1.curr=(prob.immune*probx1.immune)/totalprobx; prob.treatprotected.x1.curr=(prob.treatprotected*probx1.treatprotected)/totalprobx; prob.treatharmful.x1.curr=(prob.treatharmful*probx1.treatharmful)/totalprobx; prob.doomed.x1.curr=(prob.doomed*probx1.doomed)/totalprobx; prob.immune.x0.curr=(prob.immune*(1-probx1.immune))/(1-totalprobx); prob.treatprotected.x0.curr=(prob.treatprotected*(1-probx1.treatprotected))/(1-totalprobx); prob.treatharmful.x0.curr=(prob.treatharmful*(1-probx1.treatharmful))/(1-totalprobx); prob.doomed.x0.curr=(prob.doomed*(1-probx1.doomed))/(1-totalprobx); mean.immune.a1.curr=mean.immune.a1; sd.immune.a1.curr=sd.immune.a1; mean.immune.a0.curr=mean.immune.a0; sd.immune.a0.curr=sd.immune.a0; mean.treatprotected.a1.curr=mean.treatprotected.a1; sd.treatprotected.a1.curr=sd.treatprotected.a1; mean.treatprotected.a0.curr=mean.treatprotected.a0; sd.treatprotected.a0.curr=sd.treatprotected.a0; mean.treatharmful.a1.curr=mean.treatharmful.a1; sd.treatharmful.a1.curr=sd.treatharmful.a1; mean.treatharmful.a0.curr=mean.treatharmful.a0; sd.treatharmful.a0.curr=sd.treatharmful.a0; mean.doomed.a1.curr=mean.doomed.a1; sd.doomed.a1.curr=sd.doomed.a1; mean.doomed.a0.curr=mean.doomed.a0; sd.doomed.a0.curr=sd.doomed.a0; xeffect.curr=xeffect; maxiters=500; # Maximum iterations in EM algorithm iter=1; lldist=1; # Will measure difference in log likelihood between iterations toler=.001; # Tolerance for difference in log likelihood between iterations # for stopping EM algorithm conv=1; while(lldist>toler && iter=.00001 & sd.immune.a0.curr>=.00001 & sd.treatprotected.a1.curr>=.00001 & sd.treatprotected.a0.curr>=.00001 & sd.doomed.a1.curr>=.00001 & sd.doomed.a0.curr>=.00001 & sd.treatharmful.a1.curr>=.00001 & sd.treatharmful.a0.curr>=.00001){ lldist=ll.new-ll.old; iter=iter+1; } } if(is.na(ll.new)==TRUE){ no.notconv=no.notconv+1; conv=0; lldist=0; } } mean.immune.a1.est.1[k]=mean.immune.a1.curr; mean.immune.a0.est.1[k]=mean.immune.a0.curr; mean.treatprotected.a1.est.1[k]=mean.treatprotected.a1.curr; mean.treatprotected.a0.est.1[k]=mean.treatprotected.a0.curr; mean.treatharmful.a1.est.1[k]=mean.treatharmful.a1.curr; mean.treatharmful.a0.est.1[k]=mean.treatharmful.a0.curr; mean.doomed.a1.est.1[k]=mean.doomed.a1.curr; mean.doomed.a0.est.1[k]=mean.doomed.a0.curr; sd.immune.a1.est.1[k]=sd.immune.a1.curr; sd.immune.a0.est.1[k]=sd.immune.a0.curr; sd.treatprotected.a1.est.1[k]=sd.treatprotected.a1.curr; sd.treatprotected.a0.est.1[k]=sd.treatprotected.a0.curr; sd.treatharmful.a1.est.1[k]=sd.treatharmful.a1.curr; sd.treatharmful.a0.est.1[k]=sd.treatharmful.a0.curr; sd.doomed.a1.est.1[k]=sd.doomed.a1.curr; sd.doomed.a0.est.1[k]=sd.doomed.a0.curr; prob.immune.est.1[k]=prob.immune.curr; prob.treatprotected.est.1[k]=prob.treatprotected.curr; prob.treatharmful.est.1[k]=prob.treatharmful.curr; prob.doomed.est.1[k]=prob.doomed.curr; xeffect.est.1[k]=xeffect.curr; lldistvec.1[k]=lldist; # Expected auxiliary stratification estimates ax=aggressive.treatment*x; auxreg1=glm(esrd~x+aggressive.treatment+ax,family=binomial); muhatax=predict.glm(auxreg1,type="response"); a.times.muhatax=aggressive.treatment*muhatax; auxreg2=lm(y~muhatax+aggressive.treatment+a.times.muhatax); betahat.a[k]=coef(auxreg2)[3]; betahat.mua[k]=coef(auxreg2)[4]; # Structural nested model estimates # Use Newton's Method # Start at true values psi1.curr=mean.doomed.a1-mean.doomed.a0; psi0.curr=mean.treatprotected.a1-mean.treatprotected.a0; # Estimate "mediating score" tempreg=glm(esrd~x,family=binomial,subset=(aggressive.treatment==1)); mediatingscore=expit(coef(tempreg)[1]+coef(tempreg)[2]*x); maxiters=500; # Maximum iterations in Newton-Raphson algorithm iter=1; dist=1; toler=.001; while(itertoler){ psiold=matrix(c(psi0.curr,psi1.curr),ncol=1); y0psi=y-psi0.curr*aggressive.treatment*(1-esrd)-psi1.curr*aggressive.treatment*esrd; tempreg=lm(y0psi~x); tempbetahat0=coef(tempreg)[1]; tempbetahat1=coef(tempreg)[2]; g1=sum((aggressive.treatment-.5)*(y0psi-tempbetahat0-tempbetahat1*x)*(1-mediatingscore)); g2=sum((aggressive.treatment-.5)*(y0psi-tempbetahat0-tempbetahat1*x)*mediatingscore); Jmat=matrix(rep(0,4),ncol=2); Jmat[1,1]=sum(-(aggressive.treatment-.5)*aggressive.treatment*(1-esrd)*(1-mediatingscore)); Jmat[1,2]=sum(-(aggressive.treatment-.5)*aggressive.treatment*esrd*(1-mediatingscore)); Jmat[2,1]=sum(-(aggressive.treatment-.5)*aggressive.treatment*(1-esrd)*mediatingscore); Jmat[2,2]=sum(-(aggressive.treatment-.5)*aggressive.treatment*esrd*mediatingscore); psinew=psiold-solve(Jmat)%*%matrix(c(g1,g2),ncol=1); dist=(sum((psinew-psiold)^2))^.5; psi0.curr=psinew[1]; psi1.curr=psinew[2]; iter=iter+1; } psi0.est[k]=psi0.curr; psi1.est[k]=psi1.curr; # Estimates that assume ITT(immune)=ITT(treatment protected) # ITT(treatment harmful)=ITT(doomed) # EM algorithm # Select starting values # For now, start at truth prob.immune.curr=prob.immune; prob.treatprotected.curr=prob.treatprotected; prob.treatharmful.curr=prob.treatharmful; prob.doomed.curr=prob.doomed; prob.immune.x1.curr=(prob.immune*probx1.immune)/totalprobx; prob.treatprotected.x1.curr=(prob.treatprotected*probx1.treatprotected)/totalprobx; prob.treatharmful.x1.curr=(prob.treatharmful*probx1.treatharmful)/totalprobx; prob.doomed.x1.curr=(prob.doomed*probx1.doomed)/totalprobx; prob.immune.x0.curr=(prob.immune*(1-probx1.immune))/(1-totalprobx); prob.treatprotected.x0.curr=(prob.treatprotected*(1-probx1.treatprotected))/(1-totalprobx); prob.treatharmful.x0.curr=(prob.treatharmful*(1-probx1.treatharmful))/(1-totalprobx); prob.doomed.x0.curr=(prob.doomed*(1-probx1.doomed))/(1-totalprobx); itt.immune.treatprotected.curr=mean.immune.a1-mean.immune.a0; sd.immune.a1.curr=sd.immune.a1; mean.immune.a0.curr=mean.immune.a0; sd.immune.a0.curr=sd.immune.a0; sd.treatprotected.a1.curr=sd.treatprotected.a1; mean.treatprotected.a0.curr=mean.treatprotected.a0; sd.treatprotected.a0.curr=sd.treatprotected.a0; itt.treatharmful.doomed.curr=mean.treatharmful.a1-mean.treatharmful.a0; sd.treatharmful.a1.curr=sd.treatharmful.a1; mean.treatharmful.a0.curr=mean.treatharmful.a0; sd.treatharmful.a0.curr=sd.treatharmful.a0; sd.doomed.a1.curr=sd.doomed.a1; mean.doomed.a0.curr=mean.doomed.a0; sd.doomed.a0.curr=sd.doomed.a0; xeffect.curr=xeffect; maxiters=500; # Maximum iterations in EM algorithm iter=1; lldist=1; # Will measure difference in log likelihood between iterations toler=.001; # Tolerance for difference in log likelihood between iterations # for stopping EM algorithm conv=1; while(lldist>toler && iter