R:分组数据更改模型适合nlme?

时间:2017-03-27 00:55:33

标签: r data-structures mixed-models

> dput(mydat)
structure(list(ID = c(31L, 35L, 115L, 48L, 36L, 73L, 111L, 51L, 
113L, 20L, 16L, 51L, 59L, 79L, 107L, 90L, 60L, 72L, 21L, 28L, 
104L, 65L, 63L, 132L, 99L, 52L, 93L, 57L, 87L, 83L, 57L, 69L, 
110L, 12L, 78L, 125L, 80L, 80L, 126L, 74L, 48L, 135L, 7L, 5L, 
66L, 51L, 136L, 46L, 3L, 80L, 130L, 6L, 129L, 63L, 88L, 49L, 
60L, 71L, 42L, 89L, 106L, 128L, 114L, 82L, 103L, 8L, 67L, 130L, 
118L, 130L, 48L, 13L, 51L, 100L, 85L, 21L, 87L, 67L, 39L, 8L, 
18L, 29L, 74L, 103L, 98L, 135L, 88L, 10L, 93L, 128L, 2L, 90L, 
12L, 10L, 66L, 52L, 25L, 128L, 123L, 75L, 13L, 3L, 37L, 85L, 
53L, 13L, 10L, 76L, 93L, 68L, 40L, 36L, 29L, 109L, 96L, 120L, 
4L, 75L, 81L, 119L, 45L, 11L, 77L, 136L, 33L, 17L, 15L, 126L, 
99L, 45L, 26L, 37L, 42L, 2L, 105L, 98L, 62L, 42L, 27L, 124L, 
47L, 85L, 115L, 122L, 120L, 100L, 136L, 62L, 99L, 99L, 78L, 71L, 
93L, 118L, 28L, 103L, 43L, 64L, 56L, 124L, 128L, 103L, 82L, 138L, 
110L, 60L, 49L, 12L, 92L, 89L, 123L, 44L, 108L, 71L, 3L, 26L, 
125L, 6L, 113L, 117L, 97L, 6L, 17L, 91L, 109L, 126L, 32L, 90L, 
114L, 66L, 104L, 12L, 1L, 98L, 76L, 60L, 23L, 69L, 84L, 111L), 
    Y = c(1.50403545875011, 0.786396740696073, 4.47452220273871, 
    4.38068147273783, 3.12839926871781, 3.71525102887885, 4.91099771631064, 
    5.6099549267089, 2.56348108539441, 3.19948091486236, 2.08635983067475, 
    1.0763458953491, 1.51606413703901, 5.24654043255577, 4.52424029343984, 
    4.20774205260695, 4.12910958622073, 0.633743240652555, 4.77971190302622, 
    3.93816934639032, 1.49484285995404, 1.56126305852856, 4.46548695066214, 
    1.0084930673158, 3.04727486738418, 3.35888620440587, 3.40432046722173, 
    3.76440032295639, 4.07871050532828, 4.19226071864204, 1.7160033436348, 
    1.03724192908934, 1.58238166258979, 3.68196445899468, 3.94299959336604, 
    4.1723985779393, 1.48656664161404, 2.82216807936802, 4.29307514012286, 
    1.56346766351964, 2.82672252016899, 2.88817949391832, 1.579432355962, 
    2.75587485567249, 4.52577018572453, 4.78804103487477, 3.76900787094377, 
    4.59964294342393, 1.20237162906479, 1.54913073517381, 2.36361197989214, 
    5.29470645462496, 4.37803432245733, 1.53760300072777, 4.68198252411321, 
    4.24868424001548, 2.70586371228392, 0.795680498261033, 2.86864443839483, 
    5.05097104595277, 1.75587485567249, 1.4190891773674, 4.60685410171667, 
    2.06818586174616, 0.965391506489986, 1.64857793561105, 5.11577022280303, 
    3.23527587668705, 3.70722941932729, 1.59578845118596, 3.48826861549546, 
    4.15706370038262, 0.487678450889512, 3.22814360759774, 4.77382300021727, 
    3.69583177282669, 4.62949114909487, 4.4704545944726, 2.69108149212297, 
    3.4379090355395, 4.8963222496021, 2.03342375548695, 4.28386634847347, 
    2.83569057149243, 1.69219063796734, 4.30362797638389, 3.03981055414835, 
    5.36239952864841, 1.58185218466613, 4.36789612311481, 2.85064623518307, 
    0.684841449866386, 4.95956127990689, 4.73626101314068, 3.74036268949424, 
    4.58490763903562, 2.04139268515822, 4.33829709023269, 4.43218332439469, 
    4.84305827754328, 1.81291335664286, 1.3818767641537, 1.23195309926451, 
    3.45651785780526, 3.28375338333253, 4.76814952267996, 4.21208101599211, 
    4.61628642071719, 3.52930199778798, 3.87926795682461, 0.152221483815728, 
    3.37621185028267, 1.05830921980416, 4.56712051193916, 4.01973923267471, 
    4.52527809662657, 3.55762748842683, 6.16255544574663, 3.3392526340327, 
    4.9800761268684, 4.67728750108277, 2.77305469336426, 4.37963179601937, 
    5.08042444249764, 4.41390299750444, 1.13552331082485, 4.83799224531426, 
    0.949064591865248, 1.67706978322635, 4.95286990226433, 1.25024436906661, 
    1.66401619974254, 5.82804345699154, 3.19728055812562, 4.28768978393608, 
    3.66913084737333, 1.61566819024883, 2.77959649125782, 1.46691039598072, 
    1.3041916833582, 2.99475694458763, 1.02667774806713, 3.63346845557959, 
    4.97170714505069, 4.92332693090275, 4.34570692127843, 1.48434160170634, 
    2.78175537465247, 4.30446898485417, 3.35621713421974, 2, 
    2.01703333929878, 2.71180722904119, 4.74145100905455, 3.49262072204319, 
    4.93047527477251, 4.47468237035325, 1.79239168949825, 4.8662518850263, 
    1.49607694983225, 1.60572117915046, 4.3945392313722, 2.07918124604762, 
    1.22862414376523, 1.68741633267633, 4.06740565843782, 4.09537859956006, 
    3.53617953213723, 3.67089495352021, 4.00436437110775, 2.10720996964787, 
    3.90167623132638, 6.60281435674245, 6.51005266486288, 4.60659630917929, 
    1.6845945840158, 4.9596613702735, 1.69603672384819, 2.68841982200271, 
    1.48366771663978, 1.5218420041367, 4.65083185753557, 1.83884909073726, 
    3.05766610390983, 3.61151088712666, 3.78290240597464, 2.01283722470517, 
    3.34084054981233, 1.27334439191564, 2.32837960343874, 4.10859884597357, 
    0.864516798738721, 1.22753724273698, 3.99100444033076, 0.752257491617398, 
    5.11358574735257, 4.13624480174614, 3.87128097285797, 3.99690551069567, 
    3.05766610390983), X = c(5.7, 6.3, 4.7, 17, 0.9, 0.6, 3, 
    4.1, 6.9, 2, 11.1, 3.7, 2, 12, 1, 3.4, 8.9, 12, 12, 0, 3.9, 
    3.9, 7.9, 17, 19, 1.7, 16, 13.9, 7, 9.9, 0.9, 0, 3.6, 17.3, 
    11.7, 5, 1.7, 5.6, 8.1, 11, 3.9, 16.3, 2.1, 19.7, 19.4, 2.7, 
    0.9, 2, 15.9, 15.9, 12.1, 16.6, 14, 7.1, 1.9, 1, 18.7, 0, 
    3.9, 0.9, 8.1, 11.9, 0, 4.1, 7, 2, 2.9, 9.7, 3.6, 3.4, 9.1, 
    8.1, 13.9, 2.4, 6.9, 11.1, 4.9, 0, 9.1, 18.9, 1.9, 12, 1.1, 
    2.9, 4, 7.7, 3.9, 1.7, 2.9, 2, 6.1, 2, 5, 10.7, 14.3, 2.9, 
    2.4, 17.9, 2, 0.9, 10, 2.9, 2.9, 1.9, 8.7, 1.9, 13.4, 0, 
    14, 0.9, 2.4, 2, 8.9, 1.9, 0, 9.9, 6.9, 3.7, 0, 15.1, 0, 
    12.1, 8.9, 10.1, 0.9, 11.7, 2.7, 11.9, 10.9, 9, 0.9, 0.9, 
    9.9, 17.9, 18.9, 15, 8, 15.7, 10.9, 9.7, 13.9, 3.9, 1.9, 
    1.9, 2, 3.1, 5.1, 0.7, 15.9, 7.9, 10.1, 6.1, 5, 4.9, 6.3, 
    2, 1.7, 2.9, 13.3, 2.1, 1, 3, 0.7, 7.9, 10, 4, 4, 4, 3.9, 
    1.9, 3.9, 4.9, 4.1, 11.9, 7, 13, 7.9, 7.1, 2.6, 8.7, 10.9, 
    6.1, 16.9, 2.7, 3.3, 0.3, 16.9, 0, 6, 16.3, 1.4, 9.1, 1.9, 
    3.9, 8, 6, 8, 11.9, 0, 0.9)), .Names = c("ID", "Y", "X"), row.names = c(NA, 
-200L), class = "data.frame")

我有一个简单的数据集,包含3个变量,X,Y和主题ID。

> head(mydat)
   ID         Y    X
1  31 1.5040355  5.7
2  35 0.7863967  6.3
3 115 4.4745222  4.7
4  48 4.3806815 17.0
5  36 3.1283993  0.9
6  73 3.7152510  0.6

我运行以下非线性混合模型,它运行得很好。

library(nlme)
model <- nlme(Y ~ a*exp(X) + b,
              data = mydat,
              fixed = list(a ~ 1, b ~ 1),
              random = list(ID = pdDiag(list(a ~ 1, b ~ 1))),
              start = list(fixed = c(a = 1, b = 1)))

现在我在我的数据集上使用groupedData命令。但是,当我在groupedData对象上运行相同的精确分析时,模型不再适合。

mydat2 <- groupedData(Y ~ X | ID, data = mydat)
model2 <- nlme(Y ~ a*exp(X) + b,
              data = mydat2,
              fixed = list(a ~ 1, b ~ 1),
              random = list(ID = pdDiag(list(a ~ 1, b ~ 1))),
              start = list(fixed = c(a = 1, b = 1)))
Error in nlme.formula(Y ~ a * exp(X) + b, data = mydat2, fixed = list(a ~  : 
  step halving factor reduced below minimum in PNLS step

我不明白为什么,因为groupedData调用不应该更改mydat的内容。出了什么问题?

1 个答案:

答案 0 :(得分:0)

 mydat2 <- groupedData(Y ~ X | ID, data = mydat,order.groups=FALSE)

作为order.groups = TRUE的默认值(请参阅groupsData的帮助)。