2017-05-23 7 views
0

predict.lmeで問題が発生しました。特に、私は2つのエラーに遭遇しています。上記のコードを実行R:predict.lmeとのエラー

1.

library(nlme) 
> fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1 | Subject) 

> mydat <- structure(list(age = 8, Sex = structure(1L, .Label = "Male", class = "factor"), 
    Subject = structure(1L, .Label = "M01", class = "factor")), .Names = c("age", 
"Sex", "Subject"), row.names = c(NA, -1L), class = "data.frame") 

> predict(fm2, mydat, level = 1) 

私にエラーを与える:

> dput(ds) 
    structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4), group = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), time = c(-8, -7, -6, -5, -4, -3, -2, 
    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -8, 
    -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
    11, 12, 13, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 
    6, 7, 8, 9, 10, 11, 12, -8, -7, -6, -5, -4), logp = c(-0.106518238782883, 
    0.0460683181881905, -0.0259267091751099, -0.0920770391821861, 
    -0.0587862282991409, -0.146838054060936, 0.316394656896591, 0.386766374111176, 
    0.216139584779739, 0.496496677398682, 0.744125068187714, 1.14207077026367, 
    1.46769917011261, 2.10771918296814, 2.43389391899109, 3.00657606124878, 
    2.97612929344177, 3.03166913986206, 2.75816416740417, 2.91728544235229, 
    2.77770042419434, 3.20467519760132, 2.86933851242065, 1.84179353713989, 
    -0.817233979701996, -0.82701164484024, -0.61499285697937, -1.34559118747711, 
    -1.35250663757324, -0.834566354751587, -0.520647764205933, -0.778047442436218, 
    -0.33273184299469, -0.195379719138145, 0.0646273493766785, 0.571262776851654, 
    0.85602605342865, 1.19794964790344, 1.64863336086273, 1.57206785678864, 
    1.41767036914825, 1.53600764274597, 1.18928778171539, 1.28867197036743, 
    1.24439525604248, 0.680216789245605, -0.905854284763336, -0.503940403461456, 
    -0.496737480163574, -0.969260931015015, -0.69905811548233, -0.189700186252594, 
    -0.205862492322922, 0.0627471879124641, 0.106655828654766, 0.626791179180145, 
    0.704189598560333, 1.41776823997498, 1.77128207683563, 2.09421610832214, 
    1.91853356361389, 1.87884366512299, 1.82477164268494, 1.24088478088379, 
    1.55322957038879, 1.38231825828552, 0.904222905635834, -1.41923320293427, 
    -1.76328778266907, -1.34167373180389, -0.942831516265869, -1.47784101963043 
    )), datalabel = "", time.stamp = "24 Mar 2011 15:17", .Names = c("id", 
    "group", "time", "logp"), formats = c("%9.0g", "%9.0g", "%9.0g", 
    "%9.0g"), types = c(254L, 254L, 254L, 254L), val.labels = c("", 
    "", "", ""), var.labels = c("", "", "", ""), version = 12L, row.names = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
    "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
    "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
    "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", 
    "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", 
    "69", "70", "71", "72"), class = "data.frame") 
:別個のエラーが他のデータセットで発生

Error in predict.lme(fm2, mydat, level = 1) : 
    could not find function "MEdims" 

私は、スプラインモデル:

ds$bf1 <- (ds$time+7)*I(ds$time > -7) 
ds$bf2 <- (ds$time+3)*I(ds$time > -4) 
ds$bf3 <- (ds$time+1)*I(ds$time > -1) 
ds$bf4 <- (ds$time+2)*I(ds$time > 2) 
ds$bf5 <- (ds$time+5)*I(ds$time > 5) 
ds$bf6 <- (ds$time+8)*I(ds$time > 8) 
ds$bf7 <- (ds$time+1)*I(ds$time > 1) 
ds$bf8 <- (ds$time+14)*I(ds$time > 14) 
ds$Const <- factor(rep(1,length(ds$logp))) 

がモデルに

library(nlme) 
ds <- groupedData(logp ~ time | id, data = ds, order.groups = FALSE) 

model <- lme(logp ~ time, data = ds, 
       random=list(Const=pdIdent(~-1 + bf1 + bf2 + bf3 + bf4 + bf5 + bf6 + 
              bf7 + bf8), id=pdSymm(~time))) 
mydat <- data.frame(time = seq(-8, 15, 1), id = 1) 
predict(model, mydat, level = 1) 

フィットしかし、私は次のエラーを取得する:

Error in predict.lme(model, mydat, level = 1) : 
    Cannot evaluate groups for desired levels on "newdata" 

これはなぜでしょうか?私はtimeidの両方を指定しました。私のlmeモデルで予測を実行するために指定する必要がある他のパラメータは何ですか?

答えて

1

最初のエラーは再現できませんでしたが、2番目のケースではmydatにすべてのbfパラメータの値が含まれていません。これをコードに追加してください:

mydat$bf1 <- (mydat$time+7)*I(mydat$time > -7) 
mydat$bf2 <- (mydat$time+3)*I(mydat$time > -4) 
mydat$bf3 <- (mydat$time+1)*I(mydat$time > -1) 
mydat$bf4 <- (mydat$time+2)*I(mydat$time > 2) 
mydat$bf5 <- (mydat$time+5)*I(mydat$time > 5) 
mydat$bf6 <- (mydat$time+8)*I(mydat$time > 8) 
mydat$bf7 <- (mydat$time+1)*I(mydat$time > 1) 
mydat$bf8 <- (mydat$time+14)*I(mydat$time > 14) 
mydat$Const <- factor(rep(1,length(mydat$time))) 

predict(model, mydat, level = 1)