我正在使用nlme包中的gls函数。您可以复制并粘贴以下代码以重现我的分析。
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))
真正的分析从这里开始:
# 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)
在摘要(mod1)中,我希望检索结果的以下部分。
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
我最接近的是使用以下方法。
temp = mod1$modelStruct$varStruct
Variance function structure of class varIdent representing
1 2 3 4
1.000000 1.325880 1.370442 1.524813
然而,无论你用temp存储什么,我都无法得到五个数字。我试过as.numeric(temp)和unclass(temp),但它们都没有用。我无法将五个数字作为干净的数字向量。
提前致谢!
答案 0 :(得分:2)
在R控制台中运行mod1$modelStruct$varStruct
时,R首先检查它的类
> class(mod1$modelStruct$varStruct)
[1] "varIdent" "varFunc"
然后调度相应的print
函数。在这种情况下,它是nlme:::print.varFunc
。即,实际运行的命令是nlme:::print.varFunc(mod1$modelStruct$varStruct)
。
如果你运行nlme:::print.varFunc
,你可以看到它的功能主体
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>
它的作用是评估coef
并打印它,并且无价值地返回未评估的x
。
因此,为了得到cor / var,你需要
coef(mod1$modelStruct$corStruct, uncons = FALSE, allCoef = TRUE)
coef(mod1$modelStruct$varStruct, uncons = FALSE, allCoef = TRUE)