[R] Issue with predict() for glm models
Uwe Ligges
ligges at statistik.uni-dortmund.de
Thu Sep 23 18:37:04 CEST 2004
John Fox wrote:
> Dear Uwe,
>
>
>>-----Original Message-----
>>From: Uwe Ligges [mailto:ligges at statistik.uni-dortmund.de]
>>Sent: Thursday, September 23, 2004 8:06 AM
>>To: John Fox
>>Cc: jrausch at nd.edu; r-help at stat.math.ethz.ch
>>Subject: Re: [R] Issue with predict() for glm models
>>
>>John Fox wrote:
>>
>>
>>>Dear Uwe,
>>>
>>>Unless I've somehow messed this up, as I mentioned
>>
>>yesterday, what you
>>
>>>suggest doesn't seem to work when the predictor is a
>>
>>matrix. Here's a
>>
>>>simplified example:
>>>
>>>
>>>
>>>>X <- matrix(rnorm(200), 100, 2)
>>>>y <- (X %*% c(1,2) + rnorm(100)) > 0
>>>>dat <- data.frame(y=y, X=X)
>>>>mod <- glm(y ~ X, family=binomial, data=dat) new <- data.frame(X =
>>>>matrix(rnorm(20),2)) predict(mod, new)
>>
>>Dear John,
>>
>>the questioner had a 2 column matrix with 40 and one with 50
>>observations (not a 100 column matrix with 2 observation) and
>>for those matrices it works ...
>>
>
>
> Indeed, and in my example the matrix predictor X has 2 columns and 100 rows;
> I did screw up the matrix for the "new" data to be used for predictions (in
> the example I sent today but not yesterday), but even when this is done
> right -- where the new data has 10 rows and 2 columns -- there are 100 (not
> 10) predicted values:
>
>
>>X <- matrix(rnorm(200), 100, 2) # original predictor matrix with 100 rows
>>y <- (X %*% c(1,2) + rnorm(100)) > 0
>>dat <- data.frame(y=y, X=X)
>>mod <- glm(y ~ X, family=binomial, data=dat)
John,
note that I used glm(y ~ .) (the dot!),
because the names are automatically chosen to be X.1 and X.2, hence you
cannot use "X" in the formula in this case ...
Best,
Uwe
>>new <- data.frame(X = matrix(rnorm(20),10, 2)) # corrected -- note 10 rows
>>predict(mod, new) # note 100 predicted values
More information about the R-help
mailing list