Search code examples
rlogistic-regressionglm

How to join glm objects preserving their class ("glm" "lm")


I'm trying to estimate Nagelkerke R^2 for some logistic regression models. Since I have many models, I'm trying to automate the process with for. However, when I joined the glm models into a list, the NagelkerkeR2() function, from package fmsb, gave me an error.

##Calculate Nagelkerke's R Squared
x<- list(m01,m02,m03,m04,m05,m06,m07,m08,m09,m10)
Nag_R2 <- NA

for (i in 1:10){
   NagelkerkeR2(x[1])[2] <- Nag_R2 
}

I thing this error occured because this function just work with "glm" "lm" objects. So, is there a way to join the glm objects as a single "glm" "lm" object?


Solution

  • I think you'd be looking for something like:

    Nag_R2 <- numeric(length(x))  ## pre-allocate vector
    for (i in 1:10) {
       Nag_R2[i] <- NagelkerkeR2(x[[i]])[2]
    }
    

    This is assuming that the second component of whatever's returned by NagelkerkeR2 is a numeric value you want to save (I haven't checked).

    Note that you need to use [[]] to access elements of a list if you want an object, not a length-1 list, to be returned: from Hadley Wickham,