[R] A programming question

Marc Schwartz marc_schwartz at comcast.net
Fri May 18 18:54:41 CEST 2007


On Fri, 2007-05-18 at 09:01 -0700, Anup Nandialath wrote:
> Dear Friends,
> 
> My problem is related to how to measure probabilities from a probit
> model by changing one independent variable keeping the others
> constant. 
> 
> A simple toy example is like this
> 
> Range for my variables is defined as follows
> 
> y=0 or 1,  x1 = -10 to 10, x2=-40 to 100, x3 = -5 to 5
> 
> Model
> 
> output <- glim(y ~ x1+x2+x3 -1, family=binomial(link="probit"))
> outcoef <- output$coef
> xbeta <- as.matrix(cbind(x1, x2, x3)
> 
> predprob <- pnorm(xbeta%*%outcoef)
> 
> now I have the predicted probabilities for y=1 as defined above. My
> problem is as follows
> 
> Keep X2 at 20 and X3 at 2. Then compute the predicted probability
> (predprob) for the entire range of X1 ie from -10 to 10 with an
> increment of 1.
> 
> Therefore i need the predicted probabilities when x1=-10,
> x1=-9....,x1=9, x1=10 keeping the other constant. 
> 
> Can somebody give me some direction on how this can be programmed. 
> 
> Thanks in advance for your help
> 
> Sincerely
> 
> Anup

Anup,

What glim() function are you using? 

Or is that a typo and should be glm()?

In either case, take a look at ?predict.glm which takes your fitted
glm() model and generates predicted values based upon specifying a data
frame ('newdata' argument) containing new values.

Be sure that your 'newdata' data frame contains the same columns AND
names as the data used to fit the model.

So you could do something like:

  newdata <- data.frame(X2 = 20, X3 = 2, X1 = -10:10)

  predict(model, newdata, type = "response")

BTW, if you also want the fitted values for the actual data used to
create the model, you can use fitted(model) rather than doing the matrix
multiplications directly.  See ?fitted for more information.

HTH,

Marc Schwartz



More information about the R-help mailing list