American-Institutes-for-Research / WeMix

WeMix public repository
GNU General Public License v2.0
10 stars 1 forks source link

P value & confidence interval #10

Closed yohei-h closed 1 year ago

yohei-h commented 1 year ago

Hi All,

I would like to estimate 95% confidence intervals and p-values with WeMix::mix(). Could anybody tell me how to do it? The below is a sample code.

library(tidyverse)
library(WeMix)
library(lme4)

data(sleepstudy)
ss1 <- sleepstudy

# Create weights
ss1$W1 <- ifelse(ss1$Subject %in% c(308, 309, 310), 2, 1)
ss1$W2 <- 1

# Run random-intercept 2-level model 
two_level <- mix(Reaction ~ Days + (1|Subject), 
                 data=ss1, 
                 weights=c("W1", "W2"))

summary(two_level)  # I want 95% CI and p-value here
pdbailey0 commented 1 year ago

It depends on where the weights are from. If they are from a survey, I would estimate the df according to the appropriate survey stats methodology. If you have a large n-size, you can always use the normal approximation.

yohei-h commented 1 year ago

Hi Paul, Thank you so much for your kind advice. If I use the normal approximation for the sample above, is the way below correct?

# p_value
s <- summary(two_level)
t_value <- s$coef[, 't value'][2]
p_value <- pnorm(abs(t_value), lower.tail = F)*2

# estimate, 95% CI
estimate <- (s$coef[, 'Estimate'][2]) %>% round(2)
lower_CI <- (s$coef[, 'Estimate'][2] - 1.96*s$coef[, 'Std. Error'][2]) %>% round(2)
upper_CI <- (s$coef[, 'Estimate'][2] + 1.96*s$coef[, 'Std. Error'][2]) %>% round(2)

paste0('P=', p_value)
paste0('Estimate (95%CI): ', estimate , '(', lower_CI, ' to ', upper_CI, ')')
pdbailey0 commented 1 year ago

Yes, that's consistent with using a normal approximation.

yohei-h commented 1 year ago

Thank you so much!

yohei-h commented 1 year ago

Hi Paul,

Thank you so much for kind reply. If you have some time, I would appreciate it if you could see the #11 issue. It seems to be already closed but I have written more comments.

Yohei Hashimoto, M.D., Ph.D. | Research Fellow - Save Sight Registries Save Sight Institute | The University of Sydney Level 1 | South Block | Sydney Hospital | 8 Macquarie St | Sydney NSW 2000 Email: @. | @.

On 1 Jun 2023, at 1:42 PM, Paul Bailey @.***> wrote:

Closed #10 https://github.com/American-Institutes-for-Research/WeMix/issues/10 as completed.

— Reply to this email directly, view it on GitHub https://github.com/American-Institutes-for-Research/WeMix/issues/10#event-9397729988, or unsubscribe https://github.com/notifications/unsubscribe-auth/AK5BGPXPOYZSC7WH2M6ANFLXJAFSHANCNFSM6AAAAAAYTO3NTY. You are receiving this because you authored the thread.