[R] Using Predict and GLM

From: Sherri Rose <sherri_at_berkeley.edu>
Date: Mon, 28 Jan 2008 19:26:43 -0800


Dear R Help,

I read through the archives pretty extensively before sending this email, as it seemed there were several threads on using predict with GLM. However, while my issue is similar to previous posts (cannot get it to predict using new data), none of the suggested fixes are working.

The important bits of my code:

set.seed(644)

n0=200 #number of observations
W1=rnorm(n0,mean=2,sd=2) #Use rnorm to generate W1
W2=rnorm(n0,mean=3,sd=8) #Use rnorm to generate W1
Aprob=matrix(.2, nrow=n0, ncol=1) #generating the probability of A #generating probability of A dependant on W1 for(i in 1:n0){
if (W1[i]>1.5) {Aprob[i]=0.4}
}
A=matrix(rbinom(n0, 1, Aprob), nrow=n0, ncol=1) #generating the 0/1 exposure
Yprob=1/(1+exp(-(10*A-5*(W1)^2+2*W2)))
Y=matrix(rbinom(n0, 1, Yprob), nrow=n0, ncol=1) #generating the 0/1 exposure
zero=data.frame(rep(0, n0))

Q=glm(cbind(Y, 1-Y) ~ A + W1 + W2, family='binomial') QA=predict(Q, newdata=as.data.frame(A))
Q0=predict(Q,newdata=(A=zero))

I've tried many variations of the last line (Q0) to get the predicted values when A=0 with no luck. With this code, I get errors that my A=zero is a list even though I made it into a data frame. This is the version of the code (after my reading) that *should* work for predict once I can get it to accept that it is not a list.

With other variants of the line that will run but are not syntactically correct, my QA and Q0 are the same.

Any guidance would be appreciated!

Sherri

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 29 Jan 2008 - 03:30:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jan 2008 - 06:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive