I have a dataset with multiple timepoints, and I would like to contrast time2-time1, time3-time2, etc.
Unfortunately, the time data is being sorted as characters instead of numeric, resulting in 10 being next to 100 instead of 20 and 30.
I tried the following function:
lm_Time <- lm(measure ~ time, data = mydata)
lm_Time %>%
emmeans(consec ~ time, adjust = "dunnettx") %>%
pluck("contrasts") -> TimeContrasts
The function works, except it sorts the time column as characters instead of numeric (see below).
contrast | estimate | SE | df | t.ratio | p.value |
---|---|---|---|---|---|
time10 - time0 | -0.06926 | 0.433 | 186 | -0.160 | 1.0000 |
time100 - time10 | 0.51698 | 0.433 | 186 | 1.193 | 0.9424 |
time110 - time100 | -0.23915 | 0.433 | 186 | -0.552 | 0.9995 |
time120 - time110 | 0.06666 | 0.433 | 186 | 0.154 | 1.0000 |
time130 - time120 | -0.13622 | 0.433 | 186 | -0.314 | 1.0000 |
time140 - time130 | 0.09445 | 0.433 | 186 | 0.218 | 1.0000 |
time150 - time140 | -0.53962 | 0.433 | 186 | -1.245 | 0.9280 |
time160 - time150 | 0.17560 | 0.433 | 186 | 0.405 | 0.9999 |
time170 - time160 | -0.45169 | 0.433 | 186 | -1.042 | 0.9726 |
time180 - time170 | 0.22070 | 0.433 | 186 | 0.509 | 0.9997 |
time190 - time180 | 0.13796 | 0.433 | 186 | 0.318 | 1.0000 |
time20 - time190 | 0.19190 | 0.433 | 186 | 0.443 | 0.9999 |
time200 - time20 | 0.12803 | 0.433 | 186 | 0.295 | 1.0000 |
I have tried applying as.numeric()
to within the emmeans
, and I have also tried applying several sort commands within the emmeans
. Neither of those strategies worked.
Any help is greatly appreciated :)
The dput output is below:
structure(list(ID = c("NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7", "NSFA1", "NSFA2", "NSFA3", "NSFA4", "NSFA5",
"NSFA6", "NSFA7"), time = c("0", "0", "0", "0", "0", "0", "0",
"10", "10", "10", "10", "10", "10", "10", "20", "20", "20", "20",
"20", "20", "20", "30", "30", "30", "30", "30", "30", "30", "40",
"40", "40", "40", "40", "40", "40", "50", "50", "50", "50", "50",
"50", "50", "60", "60", "60", "60", "60", "60", "60", "70", "70",
"70", "70", "70", "70", "70", "80", "80", "80", "80", "80", "80",
"80", "90", "90", "90", "90", "90", "90", "90", "100", "100",
"100", "100", "100", "100", "100", "110", "110", "110", "110",
"110", "110", "110", "120", "120", "120", "120", "120", "120",
"120", "130", "130", "130", "130", "130", "130", "130", "140",
"140", "140", "140", "140", "140", "140", "150", "150", "150",
"150", "150", "150", "150", "160", "160", "160", "160", "160",
"160", "160", "170", "170", "170", "170", "170", "170", "170",
"180", "180", "180", "180", "180", "180", "180", "190", "190",
"190", "190", "190", "190", "190", "200", "200", "200", "200",
"200", "200", "200", "210", "210", "210", "210", "210", "210",
"210", "220", "220", "220", "220", "220", "220", "220", "230",
"230", "230", "230", "230", "230", "230", "240", "240", "240",
"240", "240", "240", "240", "250", "250", "250", "250", "250",
"250", "250", "260", "260", "260", "260", "260", "260", "260",
"270", "270", "270", "270", "270", "270", "270", "280", "280",
"280", "280", "280", "280", "280", "290", "290", "290", "290",
"290", "290", "290", "300", "300", "300", "300", "300", "300",
"300"), measure = c(1.63351915308149, 1.77874773175867, 3.50015705563717,
2.32439624137633, 2.3867235968419, 1.52099769112638, 2.12727500927918,
1.61164867026043, 1.61792828366444, 3.24946382696031, 2.69591940586543,
1.48047889731353, 2.6039168403024, 1.52764952726582, 1.3997227977924,
1.51431737410292, 2.56147122248565, 3.43364864277362, 1.7900436237908,
2.91196193028045, 1.43887322470176, 1.54086882673671, 0.674879288412828,
1.67032386923659, 1.32586875843305, 1.55071050734488, 2.50880307626376,
2.20798510204848, 1.3997227977924, 1.10183991260897, 2.67587406424878,
1.27976883573895, 1.84265098995821, 3.23205426515144, 1.31065772450765,
3.12284967113441, 1.78722703100539, 2.34069511686221, 1.11497149593301,
2.92374654058626, 1.10183991260897, 1.34840317315465, 2.0328425539765,
2.26187465285822, 2.01575691287562, 1.1366002145544, 2.24481304292639,
1.12798681094492, 2.05215784403175, 1.64588236163568, 0.788848102703265,
2.26822947635246, 1.41407494649988, 2.79612264008161, 3.2971566029204,
1.01114444121153, 2.34272179121274, 0.490670231210412, 3.35547534159059,
1.56697720910629, 3.76349049652496, 2.27245306481092, 3.34607147916713,
2.20579393251666, 0.510938075889187, 2.14099529429958, 2.47343216356879,
1.4632789901381, 1.16214289336498, 0.524281050785507, 2.47343216356879,
2.65390574185909, 2.74993445860681, 3.06105343797528, 2.01575691287562,
3.12944787442723, 2.32234810380723, 2.39460639823322, 1.14516360440023,
3.61354673134165, 3.13339592533708, 1.87769173477054, 2.99718218410231,
1.57021041547202, 2.80710002416611, 2.80710002416611, 2.32234810380723,
3.00704177887718, 1.81794375160058, 1.65815005889353, 2.77873927388457,
2.39263901736138, 1.43887322470176, 2.88066886611877, 3.33425302894405,
2.88517157922089, 1.9353384954053, 1.37793940174855, 4.06678904685626,
0.904333783554537, 1.95071681173142, 2.59164010266151, 2.43157405759104,
3.51860519251912, 1.44238347008031, 2.26399552889217, 0.391759719208298,
2.64367602470992, 2.34474611154949, 2.24052109566008, 1.02511451256556,
2.2188991165524, 1.40691564748298, 0.490670231210412, 1.79846414624875,
2.28715485377053, 2.2913325148839, 3.13339592533708, 2.94999390433702,
1.59901406500575, 0.46315712473212, 1.63041323278104, 2.18372649783188,
1.56373733250782, 2.66745615413171, 1.08858972736106, 2.3038053769111,
0.968404988487708, 2.52348814369453, 1.31447482577578, 2.12037300468822,
2.62131114755134, 0.889130408216507, 2.45832546944028, 2.43734073503924,
1.19552867313708, 2.64879810692067, 1.31065772450765, 2.32644196921343,
1.32964778894198, 3.1646915191577, 2.80710002416611, 2.33460090868772,
2.30794313445257, 1.44938009456903, 2.48280884261364, 1.3997227977924,
2.21454184581367, 0.44214095562798, 1.67939356690503, 1.07521853316776,
1.60849948302688, 3.11490171734115, 2.08072041821122, 2.28924494468403,
1.36324278630157, 2.26187465285822, 2.0255415025483, 1.80684117645744,
1.88832438338773, 1.63661899987595, 3.78350083526141, 0.626512719015478,
2.89712590600148, 2.90900413686774, 2.74185221146685, 1.99601399120726,
1.65202804664059, 1.70629759508941, 0.420784878794904, 2.82266964479624,
2.64709235591804, 1.27586287328904, 3.10692062902339, 0.563531228974086,
1.70333051302107, 0.490670231210412, 3.43477552606197, 1.61479159460257,
1.67032386923659, 1.7210511443671, 2.09013457393598, 3.57400449218521,
0.674879288412828, 2.94854531315548, 2.50880307626376, 1.48389609617481,
2.38078814285196, 1.11497149593301, 1.82346673568208, 2.0924799231334,
1.2160204292426, 2.65560509195653, 1.96342384608139, 1.58947126851799,
1.81517521048448, 1.63971279949374, 0.202489271623402, 1.81517521048448,
2.49585349398409, 2.06411794217993, 1.79285532806874, 0.963576913267833,
2.80553581934717, 0.141552843569217, 1.90151910036246, 2.5051128068377,
3.20564340068545, 2.61437354960207, 1.56697720910629, 2.45452855632671,
1.95581132475685, 1.51431737410292, 1.08414622910813, 1.83719277707321,
0.821294818608212, 3.13995801105766)), class = "data.frame", row.names = c(NA,
-217L))
CodePudding user response:
Hello Thomas and welcome to SO!
The lm()
is coercing the time variable as factor. In order to prevent that, you have to do it yourself prior to fitting the model:
> mydata$time <- factor(mydata$time, levels = unique(mydata$time))
> lm_Time <- lm(measure ~ time, data = mydata)
> lm_Time_emmeans <- emmeans(lm_Time, consec ~ time, adjust = "dunnettx")
>
> lm_Time_emmeans$contrasts
contrast estimate SE df t.ratio p.value
time10 - time0 -0.0693 0.433 186 -0.160 1.0000
time20 - time10 0.0376 0.433 186 0.087 1.0000
time30 - time20 -0.5101 0.433 186 -1.177 0.9463
time40 - time30 0.1947 0.433 186 0.449 0.9999
time50 - time40 0.1282 0.433 186 0.296 1.0000
time60 - time50 -0.1240 0.433 186 -0.286 1.0000
time70 - time60 0.0499 0.433 186 0.115 1.0000
time80 - time70 0.5595 0.433 186 1.291 0.9136
time90 - time80 -0.9510 0.433 186 -2.195 0.3813
time100 - time90 1.1321 0.433 186 2.613 0.1688
time110 - time100 -0.2392 0.433 186 -0.552 0.9995
time120 - time110 0.0667 0.433 186 0.154 1.0000
time130 - time120 -0.1362 0.433 186 -0.314 1.0000
time140 - time130 0.0945 0.433 186 0.218 1.0000
time150 - time140 -0.5396 0.433 186 -1.245 0.9280
time160 - time150 0.1756 0.433 186 0.405 0.9999
time170 - time160 -0.4517 0.433 186 -1.042 0.9726
time180 - time170 0.2207 0.433 186 0.509 0.9997
time190 - time180 0.1380 0.433 186 0.318 1.0000
time200 - time190 0.3199 0.433 186 0.738 0.9967
time210 - time200 -0.5330 0.433 186 -1.230 0.9324
time220 - time210 0.1509 0.433 186 0.348 1.0000
time230 - time220 0.4763 0.433 186 1.099 0.9630
time240 - time230 -0.5804 0.433 186 -1.340 0.8965
time250 - time240 0.0260 0.433 186 0.060 1.0000
time260 - time250 0.2801 0.433 186 0.646 0.9986
time270 - time260 -0.2186 0.433 186 -0.505 0.9997
time280 - time270 -0.3117 0.433 186 -0.719 0.9972
time290 - time280 0.5381 0.433 186 1.242 0.9290
time300 - time290 -0.2762 0.433 186 -0.637 0.9987
P value adjustment: dunnettx method for 30 tests
Hope it helps.
CodePudding user response:
Perhaps change the type from character to numeric first:
mydata$time <- as.numeric(mydata$time)
before running your model?