Hi:

> dat <- read.table(textConnection("
+  response pred1 pred2
+ 1       1     0    1
+ 2       0     0    0
+ 3       1     0    0
+ 4       1     1    1
+ 5       1     0    1
+ 6       0     1    1
+ 7       1     1    0
+ 8       1     0    1
+ 9       0     1    1
+ 10      0     0    1
+ "), header = TRUE)
> closeAllConnections()

mod <- glm(response ~ ., data = dat, family = binomial)
sdata <- dat[sample(nrow(dat), 5), -1]   # notice removal of first column!!
cbind(sdata, yhat = predict(mod, newdata = sdata))
  pred1 pred2        yhat
8     0     1  0.58116973
4     1     1 -0.08718327
5     0     1  0.58116973
7     1     0  0.26288789
3     0     0  0.93124089

(i) Use the data frame in the glm() call; it saves a lot of potential
headaches.
(ii) the columns you need in the matrix input as newdata =  in predict() are
those of the explanatory variables in the right hand side of the model
formula. In this case, the . implies that all variables other than response
are to be taken as predictors.

The reason you don't want the response column in sdata is because the
response is what you are trying to predict...

HTH,
Dennis

On Tue, Feb 8, 2011 at 7:16 PM, maxsilva <mmsilva3@uc.cl> wrote:

>
> Thank you for your kindness, but ive done what you've said and the problem
> remains.  What im doing is pretty straightforward,
>
> >data
>  response pred1 pred2
> 1       1     0    1
> 2       0     0    0
> 3       1     0    0
> 4       1     1    1
> 5       1     0    1
> 6       0     1    1
> 7       1     1    0
> 8       1     0    1
> 9       0     1    1
> 10      0     0    1
>
> >sdata <- data[sample(nrow(data), 5), ]
> > sdata
>  response pred1 pred2
> 8       1     0    1
> 2       0     0    0
> 9       0     1    1
> 10      0     0    1
> 6       0     1    1
>
> >model<-glm(data$response~data$pred1+datos$pred2,
> family=binomial(link="logit"))
> > summary(model)
> ###the model ran correctly
>
> But when I ask for the predictions,
>
> > pred<- predict(model, newdata=sdata,type="response")
> Mensajes de aviso perdidos
> 'newdata' had 5 rows but variable(s) found have 10 rows
> > length(pred)
> [1] 10
>
> And those 10 values are the fitted values corresponding for the model over
> its origin dataset.
>
> I really can't get what's the problem...
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Use-glm-coefficients-for-other-datasets-tp3276626p3296561.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

