How to retrieve correlation matrix from glm models in R

无人久伴 提交于 2020-01-03 05:12:07

问题


I am using the gls function from the nlme package. You can copy and paste the following code to reproduce my analysis.

library(nlme)  # Needed for gls function

# Read in wide format
tlc = read.table("http://www.hsph.harvard.edu/fitzmaur/ala2e/tlc.dat",header=FALSE)
names(tlc) = c("id","trt","y0","y1","y4","y6")
tlc$trt = factor(tlc$trt, levels=c("P","A"), labels=c("Placebo","Succimer"))

# Convert to long format
tlc.long = reshape(tlc, idvar="id", varying=c("y0","y1","y4","y6"), v.names="y", timevar="time", direction="long")

# Create week numerical variable
tlc.long$week = tlc.long$time-1
tlc.long$week[tlc.long$week==2] = 4
tlc.long$week[tlc.long$week==3] = 6

tlc.long$week.f = factor(tlc.long$week, levels=c(0,1,4,6))

The real analysis starts from here:

# Including group main effect assuming unstructured covariance:
mod1 = gls(y ~ trt*week.f, corr=corSymm(, form= ~ time | id), 
       weights = varIdent(form = ~1 | time), method = "REML", data=tlc.long)
summary(mod1)

In the summary(mod1), the following parts of the results are of interest to me that I would love to retrieve.

Correlation Structure: General
 Formula: ~time | id 
 Parameter estimate(s):
 Correlation: 
  1     2     3    
2 0.571            
3 0.570 0.775      
4 0.577 0.582 0.581
Variance function:
 Structure: Different standard deviations per stratum
 Formula: ~1 | time 
 Parameter estimates:
       1        2        3        4 
1.000000 1.325880 1.370442 1.524813 

The closest I can get is to use the following method.

temp = mod1$modelStruct$varStruct
Variance function structure of class varIdent representing
       1        2        3        4 
1.000000 1.325880 1.370442 1.524813 

However, whatever you stored with temp, I cannot get the five numbers out. I tried as.numeric(temp) and unclass(temp), but none of them works. There is no way I can just get the five numbers as a clean numeric vector.

Thanks in advance!


回答1:


When you run mod1$modelStruct$varStruct in R console, R first inspects the class of it

> class(mod1$modelStruct$varStruct)
[1] "varIdent" "varFunc" 

and then dispatch the corresponding print function. In this case, it is nlme:::print.varFunc. i.e., the actual command running is nlme:::print.varFunc(mod1$modelStruct$varStruct).

If you run nlme:::print.varFunc, you can see the function body of it

function (x, ...) 
{
    if (length(aux <- coef(x, uncons = FALSE, allCoef = TRUE)) > 
        0) {
        cat("Variance function structure of class", class(x)[1], 
            "representing\n")
        print(aux, ...)
    }
    else {
        cat("Variance function structure of class", class(x)[1], 
            "with no parameters, or uninitialized\n")
    }
    invisible(x)
}
<bytecode: 0x7ff4bf688df0>
<environment: namespace:nlme>

What it does is evaluating the coef and print it, and the unevaluated x is returned invisibly.

Therefore, in order to get the cor/var, you need

coef(mod1$modelStruct$corStruct, uncons = FALSE, allCoef = TRUE)
coef(mod1$modelStruct$varStruct, uncons = FALSE, allCoef = TRUE)


来源:https://stackoverflow.com/questions/23039638/how-to-retrieve-correlation-matrix-from-glm-models-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!