[R] Is glm weird or am I?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jan 27 21:16:08 CET 2005


roy wilson <rwilson+ at pitt.edu> writes:

> Hi,
> 
> I've written a script that checks all bivariate correlations for
> variables in a matrix. I'm now trying to run a logistic regression on
> each pair (x,y) where y is a factor with 2 levels. I don't know how
> (or whether I want) to try to fathom what's up with glm.
> 
> What I wrote is attached. Here's what I get.

[If you want people to debug your code, you might supply the data as
well. People might be more helpful if they can actually run your code.
Remember who is asking who for a favour...]
 
> *****************************************************
> 
> source("lrtest.R")
> building model: Wgend ~ WAY
> construct_and_run_model:
> class of x: integer nlevels(x): 0
> class of y: factor nlevels(y): 2
>   model built
>   model ran
> -1.070886   0.01171153
> building model: Wgend ~ WBWS
> construct_and_run_model:
> class of x: integer nlevels(x): 0
> class of y: factor nlevels(y): 2
>   model built
>   model ran
> 0.0837854   0.01898052
> building model: Wgend ~ Wcond
> construct_and_run_model:
> class of x: factor nlevels(x): 2
> class of y: factor nlevels(y): 2
> Error in "contrasts<-"(`*tmp*`, value = "contr.treatment") :
>         contrasts can be applied only to factors with 2 or more levels
> 
> *********************************************************************
> 
> Both Wcond and Wgend take values in {1,2}. My understanding is that,
> when family is bonomial, GLM recodes these to {0, 1}. That's
> consistent with what I've seen previously.
> 
> Excuse the possible stupidity :-).

What you're seeing is similar to this:

> x <- factor(rep(0,20),levels=0:1)
> y <- rbinom(20,1,.5)
> glm(y~x,binomial)
Error in "contrasts<-"(`*tmp*`, value = "contr.treatment") :
        contrasts can be applied only to factors with 2 or more levels

I.e. x is a two-level factor, but only one level is actually present
in data.

You have

> attach(newDataSet)
> for (cond in 1:2) {
>     # Select rows for each condition
>     t <- newDataSet[Wcond == cond,] 
....

and then you proceed to use Wcond as a regressor within the data frame
t. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list