1

I have a datset(data2) that look like this:

           Z        Y
678    -31.42962    0
1734    -31.41468   0
2567    -31.20788   0
1108    -30.43640   0
880    -30.26624    0
1599    -30.25914   0

the first column is index of Z which I have ordered them(because of the envelope package). Y is a binary var.(0 ,1 ). I am trying to do parametic bootstrap with logistic regression being my model and my goal is to finaly plot the confidence interval for it. here is the code:

# parametric bootstrap
library(boot)

mle =glm(formula = Y~ Z, data = data2, family =binomial)

rng = function(data,mle){
    data1 = data.frame(Y = data$Y,Z = data$Z )
    n = length(data$Y)
    # generate new PC1
    data1$Y = rbinom(n= n,prob = predict.glm(mle,newdata = data1,type='response'),size = 1)     
    return(data1)
    }

f1 = function(data1){                        

    res =  glm(formula = Y~ Z, data = data1, family =binomial)
    #predict crime for all PC1 from original data
    YP = predict(res,newdata=data2)
    return(YP)
}
          res = boot(data2, statistic = f1,R = 3000, mle = mle,ran.gen = rng,sim = 'parametric')  

       

then I use the 'envelope' package to compute confidence band,

e <- envelope(res)

and finally, I plot them:

fit = glm(formula = Y~ Z, data = data2, family =binomial)
YP = predict.glm(fit,type='response')
plot(y=data2$Y, x=data2$Z, pch=21, bg="orange")
points(x=data2$Z,y=YP,type="l") #plot fitted line
#plot cofidence bands
points(x=data2$Z,e$point[2,], type="l", col="blue")
points(x=data2$Z,e$point[1,], type="l", col="blue")

and here is the result of the plot: enter image description here

I don't get where I'm wrong I did the same process with Linear regression and envelope everything worked! Anybody knows where I am wrong!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.