我可以使用协方差矩阵来指定 nlme 函数 gls 中的相关结构吗?

Can I use a covariance matrix to specify the correlation structure in the nlme function gls?

我希望使用 R 包 nlme 中的函数 gls 来分析一组嵌套空间样本,其中许多样本至少在某些空间坐标上重叠。我想使用 corStruct 或 pdMat 对象来解释响应变量(我在每个空间样本中测量的东西)中的非独立性,但我对如何执行此操作感到困惑。

我已经生成了一个协方差矩阵,它应该对所有关于空间样本之间的非独立性的信息进行编码。每个 row/column 是一个不同的空间样本,对角线包含每个空间样本捕获的采样单元总数,非对角线元素包含空间样本之间共享的采样单元的计数。

我想我应该在指定相关结构时使用 nlme 函数 gls,可能使用 corSymm 或 pdMat 对象。但我只看到过 gls 中的相关结构是通过公式指定的示例。如何使用我创建的协方差矩阵?

我发现您可以使用 corSymm 提供的一般相关结构向 nlme 函数 gls 传递一个正定相关矩阵。

# convert your variance covariance matrix into a correlation matrix
CM <- cov2cor(vcv_matrix)

# if your correlation matrix contains zeros, as mine did, you need to convert it to a positive-definite matrix that substitutes very small numbers for those zeros
CM <- nearPD(CM)$mat

# convert into a corStruct object using general correlation structure provided by corSymm
C <- corSymm(CM[lower.tri(CM)], fixed = T)

# correlation structure can now be included in a gls model
gls(y ~ x, correlation = C, method = "ML")