简体   繁体   中英

How to do longitudinal analysis for 2x2x2 design in R?

I have a dataset with the following variables for the treatment (nutrition, fertilizer), that records algal growth in water across time (t0, t1...t10). In series with fertilizer marked as "nitrogen" , nitrogen was added after day t5. In series marked as "none", no nitrogen was added.

nutrition <- c("good","good","bad","bad","good","good","bad","bad","good","good","bad","bad","good","good","bad","bad")

fertlizer <- c("none", "nitrogen","none","nitrogen","none", "nitrogen","none","nitrogen","none", "nitrogen","none","nitrogen","none", "nitrogen","none","nitrogen")

t0 <- c(7,  6,  3, 20, 13,  4, 14,  9, 15,  5, 18, 19,  8,  1, 10, 16)
t1 <- c(12,  9,  3, 20,  4,  7,  6, 17, 19,  5, 18,  8, 15, 16, 10,  2)
t2 <- c(12,  9,  3, 20,  4,  7,  6, 17,7,  6,  3, 20, 13,  4, 14,  9)
t3 <- c(15,  5, 18, 19,  8,  1, 10, 16,4,  7,  6, 17,7,  6,  3, 20)
t4 <- c(6,7,12,4,7,18,9,10,2,10,11,14,15,1,14,16)
t5 <- c(4,  7,  6, 17,7,  6,  3, 20,15,  5, 18, 19,  8,  1, 10, 16)
t6 <- c(70,5,16,31,61,14,22,23,80,13,24,32,90,16,28,29)
t7 <- c(56,16,7,8,78,26,28,30,91,5,8,19,67,16,18,19)
t8 <- c(88,21,20,19,90,16,18,19,57,3, 20,  4,  7,67,13,12)
t9 <- c(62,12,15,27,71,20,  4,  7,72,6,  3, 20,73,14,  9, 15)
t10 <- c(40,13,7,19,50,3, 20, 7,66,14,  9, 15,80,16,18,19)


replicates <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

data <- data.frame(nutrition, fertlizer,replicates, t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10)

data$nutrition <- as.factor(data$nutrition)
data$fertlizer <- as.factor(data$fertlizer)

I want to compare the slopes between groups and see if the slopes change after the fertilizer intervention. I want to use no fertilizer as control (ie (good, none) or (bad, none) as controls)

I transform this data into long format with column headers as "nutrition", "fertilizer", "time", "replicates", and "growth" I create a new column called addition to distinguish between time periods before t5 and after t5. Time before t5 -> 0, after t5 ->1

   nutrition fertilizer time replicate growth addition
   good      none      t0   1         6        0
   good      none      t1   1         7        0
   ..
   ..
   good      none      t5   1         3        1

I run the following longitudinal analysis, with each column having the following structure:

nutrition: factor with 2 levels
fertlizer: factor with2 levels
time: factor with 10 levels
replicates: num 0,1,2,3...
growth: num 6, 7, 5 ...
addition: factor with 2 levels

lmer(growth ~ nutrition + fertlizer + time + addition + (1|replicates))

I get an errror message saying that fixed effects model is rank deficient so dropping x number of columns. Is there anyway around this problem? Are there suggestions to improve the way the model is written?

From what I know, that error message means your predictors don't have enough information for your desired model to run. Perhaps you should consider using an ANOVA or ANCOVA with a block? It would be totally fine to go this route considering the experimental nature of your data. You should also create a new column with your treatment groups. For example good nutrition w/ fertilizer, good nutrition w/o fertilizer can be called GWF and GWOF , same goes with the bad nutrition combos. This would give you 4 treatments and your block could be "time".

Posting a sample of your dataset would help in solving your problem

I'm not 100% sure on this, but I believe it should get you somewhere in the ballpark.

dtf <- structure(list(nutr = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("bad", "good"
), class = "factor"), fert = structure(c(2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("N", 
"0"), class = "factor"), id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 13, 14, 15, 16), t0 = c(7, 6, 3, 20, 13, 4, 14, 9, 
15, 5, 18, 19, 8, 1, 10, 16), t1 = c(12, 9, 3, 20, 4, 7, 6, 17, 
19, 5, 18, 8, 15, 16, 10, 2), t2 = c(12, 9, 3, 20, 4, 7, 6, 17, 
7, 6, 3, 20, 13, 4, 14, 9), t3 = c(15, 5, 18, 19, 8, 1, 10, 16, 
4, 7, 6, 17, 7, 6, 3, 20), t4 = c(6, 7, 12, 4, 7, 18, 9, 10, 
2, 10, 11, 14, 15, 1, 14, 16), t5 = c(4, 7, 6, 17, 7, 6, 3, 20, 
15, 5, 18, 19, 8, 1, 10, 16), t6 = c(70, 5, 16, 31, 61, 14, 22, 
23, 80, 13, 24, 32, 90, 16, 28, 29), t7 = c(56, 16, 7, 8, 78, 
26, 28, 30, 91, 5, 8, 19, 67, 16, 18, 19), t8 = c(88, 21, 20, 
19, 90, 16, 18, 19, 57, 3, 20, 4, 7, 67, 13, 12), t9 = c(62, 
12, 15, 27, 71, 20, 4, 7, 72, 6, 3, 20, 73, 14, 9, 15), t10 = c(40, 
13, 7, 19, 50, 3, 20, 7, 66, 14, 9, 15, 80, 16, 18, 19)), .Names = c("nutr", 
"fert", "id", "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", 
"t8", "t9", "t10"), row.names = c(NA, -16L), class = "data.frame")

# Need to reshape into long format so that each column is a separate variable
library(reshape2)

dtf.long <- reshape2::melt(dtf, id.vars=1:3, variable.name="time")
dtf.long$time <- as.integer(sub("t", "", dtf.long$time))
dtf.long$fert2 <- dtf.long$time > 5 & dtf.long$fert == "N"

library(lattice)
xyplot(value ~ time | nutr * fert, data=dtf.long )

library(lme4)
m1.1 <- lmer(value ~ nutr * fert2 * time + (1 | id), dtf.long, REML=FALSE)
m1.2 <- lmer(value ~ nutr * fert2 * time + (1 + time | id), dtf.long, REML=FALSE)

# The random slope term doesn't appear to be adding anything of value
anova(m1.1, m1.2)

These slides on longitudinal modelling in lme4 might be of use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM