# Re: [R] A programming question

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Fri, 18 May 2007 11:54:41 -0500

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.
>
>
> 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

R-help_at_stat.math.ethz.ch 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 Fri 18 May 2007 - 17:00:37 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 Fri 18 May 2007 - 17:31:53 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.