开发者

Why do I get an error when I try to model autocorrelation, even when exactly following this example in Pinheiro and Bates (2009)?

开发者 https://www.devze.com 2023-03-06 14:05 出处:网络
Here is an excerpt from Mixed Effects Models in S and S-Plus page 238: this is the code that I have used to recreate this example:

Here is an excerpt from Mixed Effects Models in S and S-Plus page 238:


Why do I get an error when I try to model autocorrelation, even when exactly following this example in Pinheiro and Bates (2009)?

Why do I get an error when I try to model autocorrelation, even when exactly following this example in Pinheiro and Bates (2009)?


this is the code that I have used to recreate this example:

library(nlme)
spatDat <- data.frame(x = c(开发者_Python百科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)

but when I do so, I get this error:

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

Why do I get this error?


Appendix

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


The reason is that Initialize takes a capital I in nlme so it is not confused with initialize in base. And then there is Vivi's comment on spatdat$y

This works:

> 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


There are a couple of problems with your code. Here's the corrected version:

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


To emphasize the quality of the documentation (and hopefully to save someone else time when working with such a well documented textbook package), I will point out that the code in question is provided in the help file under ?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)
     ...
0

精彩评论

暂无评论...
验证码 换一张
取 消