2017-05-05 9 views
1

私はlmerモデルでいくつかの固定係数をテストしていますが、それ以降のプロセス(各変数の寄与率を計算する)でそのモデルを使用する必要があるため、 lmerModモデル。

私は次のエラーメッセージに

エラーオブジェクト@の頁の$ Xを変更しているに苦しんだワン:無効な交換:参照クラスのフィールド「X」は読み取り専用です

再現例を以下に:

#Load package and data 
library(lme4) 
data(iris) 

#build the model 
mod<-lmer(Sepal.Length~Petal.Length + offset(Petal.Width*1) + (1|Species),data=iris) 

fixef(mod) #not showing the offset coefficient 

#apply changes to mod to get fixef(mod) to work with new coefficient 
[email protected] <- c([email protected],1) #because model was offset by 1*Petal.Width 
[email protected]$X <- matrix(data.frame([email protected]$X, iris["Petal.Width"])) #causes the error 

#check fixef: 
fixef(mod) # should have Petal.Width at the end with a value of 1 

は注意するには、次の

[fixef] :(https://github.com/lme4/lme4/blob/master/R/lmer.R#L876)!

  1. は、@ betaに(すでに正常に変更されています);
  2. はgetME(オブジェクト、 "X"):(https://github.com/lme4/lme4/blob/master/R/lmer.R#L1932)です。

私は彼らの変数名(直接lmerModを調整することができること)事前に

おかげでfixef係数に取得する別の方法に開いています!

答えて

1

オブジェクトmodのスロットppのクラスがmerPredDある:

library(lme4) 
data(iris) 
mod <- lmer(Sepal.Length~Petal.Length + offset(Petal.Width*1) + (1|Species),data=iris) 
class(slot(mod,"pp")) 

[1] "merPredD" 
attr(,"package") 
[1] "lme4" 

lme4オブジェクトのこのクラスはlme4merPredDコマンドによって生成することができます。ここ
[email protected]の同じパラメータを使用してmerPredDオブジェクトを生成するための例です:

[email protected] <- obj1を使用して
obj1 <- merPredD([email protected]$X, [email protected]$Zt, [email protected]$Lambdat, [email protected]$Lind, 
     [email protected]$theta, n=nrow([email protected]$X)) 
class(obj1) 

我々はエラーメッセージを取得していないと我々はobj1[email protected]オブジェクトを置き換えることができます。我々はまた、[email protected]$Xに3番目の列を追加することができます

mod <- lmer(Sepal.Length~Petal.Length + offset(Petal.Width*1) + (1|Species),data=iris) 
Xold <- Xnew <- [email protected]$X 
set.seed(1) 
Xnew[,2] <- rnorm(nrow(Xold)) 
[email protected] <- merPredD(X=Xnew, [email protected]$Zt, [email protected]$Lambdat, [email protected]$Lind, 
     [email protected]$theta, n=nrow([email protected]$X)) 
head(cbind(Xold[,2], [email protected]$X[,2])) 

####### 
    [,1]  [,2] 
1 1.4 -0.6264538 
2 1.4 0.1836433 
3 1.3 -0.8356286 
4 1.5 1.5952808 
5 1.4 0.3295078 
6 1.7 -0.8204684 

mod <- lmer(Sepal.Length~Petal.Length + offset(Petal.Width*1) + (1|Species),data=iris) 
Xnew <- as.matrix(data.frame([email protected]$X, iris["Petal.Width"])) 
colnames(Xnew) <- c(colnames([email protected]$X),"Petal.Width") 
[email protected] <- merPredD(X=Xnew, [email protected]$Zt, [email protected]$Lambdat, [email protected]$Lind, 
     [email protected]$theta, n=nrow([email protected]$X)) 
head([email protected]$X) 

####### 
    (Intercept) Petal.Length Petal.Width 
1   1   1.4   0.2 
2   1   1.4   0.2 
3   1   1.3   0.2 
4   1   1.5   0.2 
5   1   1.4   0.2 
6   1   1.7   0.4 
+0

感謝マルコ我々は例えば[email protected]$Xの2番目の列を変更することができ、このように続い
。これは完全にうまくいった –

+0

こんにちはマルコ、おそらく解決策を提供することができます[**このlme4質問**](https://stackoverflow.com/questions/49120251/specification-of-a-multilevel-model-in-package- lme4-in-r)? – rnorouzian

関連する問題