R: Generating separate toolpaths using lme

> 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")

      

I have a dataset here with 72 rows (24 per person) and I am interested in a mixed spline model / fine setup.

> head(ds)
  id group time        logp
1  1     0   -8 -0.10651824
2  1     0   -7  0.04606832
3  1     0   -6 -0.02592671
4  1     0   -5 -0.09207704
5  1     0   -4 -0.05878623
6  1     0   -3 -0.14683805

      

First I create some truncated linear functions:

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)))

      

Install the model

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))) 
summary(model)
plot(augPred(model, level=0:1))

      

I am interested in designing separate paths (this is the way I called augPred

). However, the result looks like this: enter image description here

This is not what I am looking for. Instead, I can manually multiply the data points corresponding to ID = 1

plot(fitted(model)[1:24])

enter image description here

It is tedious and does not work when there is imbalanced data.

ds2 = ds[-c(seq(1, 23, 2)),]
model2 <- lme(logp ~ time, data = ds2,
             random=list(Const=pdIdent(~-1 + bf1 + bf2 + bf3 + bf4 + bf5 + bf6 + 
                                         bf7 + bf8), id=pdSymm(~time))) 

plot(fitted(model2)[1:24])

      

Here I have removed the odd time points from ID 1 and using the same line of code I generated no longer matches the individual trajectory of ID 1.

Ideally, I would just like to name augPred

and have a panel per cluster (in this case, my cluster is the ID), and in each panel I have a fixed path (blue) and an individual path (pink) (that is, something that looks like So)

> fm1 <- lme(distance ~ age, data = Orthodont) # random is ~ age
> plot(augPred(fm1, level = 0:1))

      

enter image description here

+3


source to share





All Articles