CecileProust-Lima / lcmm

R package lcmm
https://CecileProust-Lima.github.io/lcmm/
58 stars 13 forks source link

contrast after predictY #278

Closed HaibinLi0817 closed 1 month ago

HaibinLi0817 commented 1 month ago

Hi,

Using the predictY function, I am able to generate predicted trajectories based on a profile of covariates. However, I am uncertain how to contrast the difference (with 95% confidence intervals) between two groups using these predictions. Could you please provide guidance on how to perform this comparison and plot the results, including the 95% CI?

datnew <- data.frame(age=seq(65,95,length=100)) datnew$age65 <- (datnew$age - 65)/10 datnew$male <- 0 women <- predictY(mspl5q, newdata=datnew, var.time="age", draws=TRUE) datnew$male <- 1 men <- predictY(mspl5q, newdata=datnew, var.time="age", draws=TRUE)

Best, Haibin

HaibinLi0817 commented 1 month ago

image When I summarise the results from the longitudinal model, I obtain the fixed effects. For example, when age 65 is set to 0, the coefficient for male is -0.83140. Can I interpret this as meaning the mean difference in CESD score was -0.83 at age 65? However, this result differs from the predicted value obtained using predictY (difference: -3.042017). cbind(women$times,women$pred) image cbind(men$times,men$pred) image men$pred[1]-women$pred[1] image image

VivianePhilipps commented 1 month ago

Hi,

the fixed effects you get are not in the original scale of the outcome, but in the transformed scaled (the transformation is the link function). The predictY function goes back to the original scale, that's why the effect is different.

If you want a CI around this difference, you can't get it directly, but you can compute it (almost by hand) using the predictY function. Here is an example

datnew0 <- data.frame(age = (seq(65, 95, length = 100) - 65) / 10, male = 0)
datnew1 <- data.frame(age = (seq(65, 95, length = 100) - 65) / 10, male = 1)

ndraws <- 2000
diff <- matrix(NA, nrow(datnew0), ndraws) # will contain the differences

for(k in 1:ndraws)
{
  # prediction for women
  set.seed(k)
  p0 <- predictY(m, newdata=datnew0, draws = TRUE, ndraws = 1)

  # prediction for men
  set.seed(k)
  p1 <- predictY(m, newdata=datnew1, draws = TRUE, ndraws = 1)

  # the difference
  diff[, k] <- p0$pred[,1] - p1$pred[,1]
}

# get the predicted difference and its CI
diff100 <- apply(diff, 1, quantile, prob = c(0.025, 0.5, 0.975))
HaibinLi0817 commented 1 month ago

Hi,

the fixed effects you get are not in the original scale of the outcome, but in the transformed scaled (the transformation is the link function). The predictY function goes back to the original scale, that's why the effect is different.

If you want a CI around this difference, you can't get it directly, but you can compute it (almost by hand) using the predictY function. Here is an example

datnew0 <- data.frame(age = (seq(65, 95, length = 100) - 65) / 10, male = 0)
datnew1 <- data.frame(age = (seq(65, 95, length = 100) - 65) / 10, male = 1)

ndraws <- 2000
diff <- matrix(NA, nrow(datnew0), ndraws) # will contain the differences

for(k in 1:ndraws)
{
  # prediction for women
  set.seed(k)
  p0 <- predictY(m, newdata=datnew0, draws = TRUE, ndraws = 1)

  # prediction for men
  set.seed(k)
  p1 <- predictY(m, newdata=datnew1, draws = TRUE, ndraws = 1)

  # the difference
  diff[, k] <- p0$pred[,1] - p1$pred[,1]
}

# get the predicted difference and its CI
diff100 <- apply(diff, 1, quantile, prob = c(0.025, 0.5, 0.975))

Thanks for your help~