Calculating confidence intervals using the expss package

28 Views Asked by At

I'm using expss tables to calculate means values. I can't find a function to report confidence intervals around the means.

I know r has several packages that can calculate and report confidence intervals, but I'm focused on using the expss package. I tried to create a function using tab_stat_fun_df() with no success. I also reviewed the significance documentation for the expss package hoping to have an option that reports CI.

Looking to add CI for the means in this code:

df <- data.frame(Group=c(1,1,1,1,1,1,1,0,0,0,0,0,0,0,0),
                 Response=c(0,1,0,1,0,1,0,1,0,1,0,1,1,0,0),
                 Value=c(10,20,17,14,15,16,17,18,19,10,11,12,13,14,15))

library(expss)

df %>% tab_rows(Group) %>% tab_cols(Response) %>% tab_cells(Value) %>%
       tab_stat_mean_sd_n() %>% tab_pivot()
1

There are 1 best solutions below

0
Marco Sandri On BEST ANSWER

What I propose is not an actual solution, but rather a starting point (based on the use of the tab_stat_fun_df command). Some further refinement is needed to achieve what you need.

library(expss)
df <- data.frame(Group=c(1,1,1,1,1,1,1,0,0,0,0,0,0,0,0),
                 Response=c(0,1,0,1,0,1,0,1,0,1,0,1,1,0,0),
                 Value=c(10,20,17,14,15,16,17,18,19,10,11,12,13,14,15))

df %>% tab_rows(Group) %>% 
       tab_cols(Response) %>% 
       tab_cells(Value) %>%
       tab_stat_mean_sd_n() %>% 
       tab_stat_fun_df(function(x) { 
                       out <- t.test(x)
                       mn <- round(out$estimate,2)
                       CI=round(out$conf.int,2)
                       c("Mean" = paste0(mn, " (", CI[1], " - ", CI[2], ")"))
                       }) %>%
       tab_pivot()

The output is

 |       |    |       |              |             Response |                      |
 |       |    |       |              |                    0 |                    1 |
 | ----- | -- | ----- | ------------ | -------------------- | -------------------- |
 | Group |  0 | Value |         Mean |                14.75 |                13.25 |
 |       |    |       |    Std. dev. |     3.30403793359983 |     3.40342964277702 |
 |       |    |       | Unw. valid N |                    4 |                    4 |
 |       |  1 | Value |         Mean |                14.75 |     16.6666666666667 |
 |       |    |       |    Std. dev. |     3.30403793359983 |     3.05505046330389 |
 |       |    |       | Unw. valid N |                    4 |                    3 |
 |       |  0 |  Mean |              | 14.75 (9.49 - 20.01) | 13.25 (7.83 - 18.67) |
 |       |  1 |  Mean |              | 14.75 (9.49 - 20.01) | 16.67 (9.08 - 24.26) |