当我尝试建模自相关时,为什么会出现错误,即使在Pinheiro和Bates(2009)中完全遵循这个例子?

时间:2011-05-16 22:36:34

标签: r

以下是Mixed Effects Models in S and S-Plus页面238的摘录:


enter image description here enter image description here


这是我用来重新创建此示例的代码:

library(nlme)
spatDat <- data.frame(x = c(0,0.25,0.5,0.75,1), y = c(0,0.25,0.5,0.50,0.75))
cs1Exp <- corExp(1, form = ~x+y)
cs1Exp <- initialize(cs1Exp, spatDat)

但是当我这样做时,我收到此错误:

Error in getClass(Class) : 
  c("\"corExp\" is not a defined class", "\"corSpatial\" is not a defined class", "\"corStruct\" is not a defined class")
In addition: Warning message:
In if (!is.na(match(Class, .BasicClasses))) return(newBasic(Class,  :
  the condition has length > 1 and only the first element will be used

为什么会出现此错误?


附录

R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] nlme_3.1-101

loaded via a namespace (and not attached):
[1] grid_2.13.0     lattice_0.19-26

3 个答案:

答案 0 :(得分:10)

原因是InitializeI中占用了nlme,因此不会与initialize中的base混淆。然后是Vivi对spatdat$y

的评论

这有效:

> library(nlme)
> spatDat <- data.frame(x = c(0,0.25,0.5,0.75,1), y = c(0,0.25,0.50,0.75,1.0))
> cs1Exp <- corExp( 1, form = ~x+y )
> cs1Exp <- Initialize( cs1Exp, spatDat )
> corMatrix( cs1Exp )
          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 1.0000000 0.7021885 0.4930687 0.3462272 0.2431167
[2,] 0.7021885 1.0000000 0.7021885 0.4930687 0.3462272
[3,] 0.4930687 0.7021885 1.0000000 0.7021885 0.4930687
[4,] 0.3462272 0.4930687 0.7021885 1.0000000 0.7021885
[5,] 0.2431167 0.3462272 0.4930687 0.7021885 1.0000000

答案 1 :(得分:5)

您的代码存在一些问题。这是更正后的版本:

library(nlme)    
spatDat <- data.frame(x = c(0, 0.25, 0.5, 0.75, 1), y = c(0, 0.25, 0.5, 0.75, 1.0))    
cs1Exp <- corExp(1, form = ~x+y)    
cs1Exp <- Initialize(cs1Exp, spatDat)  
corMatrix(cs1Exp)  

          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 1.0000000 0.7021885 0.4930687 0.3462272 0.2431167
[2,] 0.7021885 1.0000000 0.7021885 0.4930687 0.3462272
[3,] 0.4930687 0.7021885 1.0000000 0.7021885 0.4930687
[4,] 0.3462272 0.4930687 0.7021885 1.0000000 0.7021885
[5,] 0.2431167 0.3462272 0.4930687 0.7021885 1.0000000

答案 2 :(得分:3)

为了强调文档的质量(并希望在处理这样一个记录良好的教科书包时节省其他人的时间),我将指出有问题的代码在?corExp下的帮助文件中提供。

Examples:

     # Pinheiro and Bates, p. 238
     spatDat <- data.frame(x = (0:4)/4, y = (0:4)/4)

     cs1Exp <- corExp(1, form = ~ x + y)
     cs1Exp <- Initialize(cs1Exp, spatDat)
     corMatrix(cs1Exp)
     ...
相关问题