Reframing output of confidence intervals to combine mean, upper and lower values into one cell

huangapple go评论88阅读模式
英文:

Reframing output of confidence intervals to combine mean, upper and lower values into one cell

问题

I use the code below to calculate the mean, upper and lower confidence intervals of multiple variables at once.

  1. library(gmodels)
  2. library(purrr)
  3. dfci <- df %>%
  4. group_by(group) %>%
  5. dplyr::summarize(across(everything(),
  6. .fns = list(mean = ~ mean(.x, na.rm = TRUE, trim = 4),
  7. ci = ~ ci(.x, confidence = 0.95, alpha = 0.05, na.rm = T))))
  8. #dfci <- dfci[-(13:16),] # remove additional rows
  9. write.csv(dfci, file="dfci.csv")

Sample data:

  1. Group| A_pre | A_post | B_pre | B_post
  2. 0 20 21 20 23
  3. 1 30 10 19 11
  4. 2 10 53 30 34
  5. 1 22 32 25 20
  6. 2 34 40 32 30
  7. 0 30 50 NA 40
  8. 0 39 40 19 20
  9. 1 40 NA 20 20
  10. 2 50 10 20 10
  11. 0 34 23 30 10

I tried pivoting into long after the ci calculations but doesn't work:

  1. library(reshape2)
  2. dfci <- df %>%
  3. group_by(group) %>%
  4. summarize(across(everything(),
  5. .fns = list(mean = ~ mean(.x, na.rm = TRUE, trim = 4),
  6. ci = ~ ci(.x, confidence = 0.95, alpha = 0.05, na.rm = TRUE))))
  7. dfci <- melt(dfci, id.vars = "group")
  8. dfci <- dcast(dfci, group + variable ~ variable)
  9. write.csv(dfci, file = "dfi.csv", row.names = FALSE)
英文:

I use the code below to calculate the mean, upper and lower confidence intervals of multiple variables at once.

  1. library(gmodels)
  2. library(purrr)
  3. dfci &lt;- df %&gt;%
  4. group_by(group) %&gt;%
  5. dplyr::summarize(across(everything(),
  6. .fns = list(mean = ~ mean(.x, na.rm = TRUE, trim = 4),
  7. ci = ~ ci(.x, confidence = 0.95, alpha = 0.05, na.rm = T))))
  8. #dfci &lt;- dfci[-(13:16),] # remove additional rows
  9. write.csv(dfci, file=&quot;dfci.csv&quot;)

Sample data:

  1. Group| A_pre | A_post | B_pre | B_post
  2. 0 20 21 20 23
  3. 1 30 10 19 11
  4. 2 10 53 30 34
  5. 1 22 32 25 20
  6. 2 34 40 32 30
  7. 0 30 50 NA 40
  8. 0 39 40 19 20
  9. 1 40 NA 20 20
  10. 2 50 10 20 10
  11. 0 34 23 30 10

As I have over 50 "pre" and "post" variables i.e., >100 variables, is it possible to combine the outputs from the three desired cells (mean, lower and upper ci) into one so I am not manually combining all of them?

I tried pivoting into long after the ci calculations but doesn't work:

  1. library(reshape2)
  2. dfci &lt;- df %&gt;%
  3. group_by(group) %&gt;%
  4. summarize(across(everything(),
  5. .fns = list(mean = ~ mean(.x, na.rm = TRUE, trim = 4),
  6. ci = ~ ci(.x, confidence = 0.95, alpha = 0.05, na.rm = TRUE))))
  7. dfci &lt;- melt(dfci, id.vars = &quot;group&quot;)
  8. dfci &lt;- dcast(dfci, group + variable ~ variable)
  9. write.csv(dfci, file = &quot;dfi.csv&quot;, row.names = FALSE)

答案1

得分: 2

以下是您提供的内容的翻译:

更新后的澄清:

我们可以使用自定义的ci函数来使用reframe:

  1. library(dplyr) #&gt;= dplyr 1.1.0
  2. df %&gt;%
  3. reframe(across(everything(), .fns = list(
  4. mean = ~ mean(.x, na.rm = TRUE, trim = 4),
  5. ci = ~ {
  6. se &lt;- sqrt(var(.x, na.rm = TRUE) / sum(!is.na(.x)))
  7. mean_val &lt;- mean(.x, na.rm = TRUE)
  8. lower &lt;- mean_val - qt(0.975, df = sum(!is.na(.x))) * se
  9. upper &lt;- mean_val + qt(0.975, df = sum(!is.na(.x))) * se
  10. # c(lower, upper)
  11. paste0(&quot;[&quot;, round(lower, 2), &quot;, &quot;, round(upper, 2), &quot;]&quot;)
  12. }
  13. )), .by = Group)
  1. Group A_pre_mean A_pre_ci A_post_mean A_post_ci B_pre_mean B_pre_ci B_post_mean B_post_ci
  2. 1 0 32 [19.56, 41.94] 31.5 [14.18, 52.82] 20 [11.82, 34.18] 21.5 [5.93, 40.57]
  3. 2 1 30 [14.1, 47.24] 21.0 [-26.33, 68.33] 20 [15.43, 27.24] 20.0 [7.45, 26.55]
  4. 3 2 34 [-5.66, 68.33] 40.0 [-6.19, 74.85] 30 [15.52, 39.15] 30.0 [1.04, 48.29]
英文:

Update after clarification:

We can use reframe with a custom ci function:

  1. library(dplyr) #&gt;= dplyr 1.1.0
  2. df %&gt;%
  3. reframe(across(everything(), .fns = list(
  4. mean = ~ mean(.x, na.rm = TRUE, trim = 4),
  5. ci = ~ {
  6. se &lt;- sqrt(var(.x, na.rm = TRUE) / sum(!is.na(.x)))
  7. mean_val &lt;- mean(.x, na.rm = TRUE)
  8. lower &lt;- mean_val - qt(0.975, df = sum(!is.na(.x))) * se
  9. upper &lt;- mean_val + qt(0.975, df = sum(!is.na(.x))) * se
  10. # c(lower, upper)
  11. paste0(&quot;[&quot;, round(lower, 2), &quot;, &quot;, round(upper, 2), &quot;]&quot;)
  12. }
  13. )), .by = Group)
  1. Group A_pre_mean A_pre_ci A_post_mean A_post_ci B_pre_mean B_pre_ci B_post_mean B_post_ci
  2. 1 0 32 [19.56, 41.94] 31.5 [14.18, 52.82] 20 [11.82, 34.18] 21.5 [5.93, 40.57]
  3. 2 1 30 [14.1, 47.24] 21.0 [-26.33, 68.33] 20 [15.43, 27.24] 20.0 [7.45, 26.55]
  4. 3 2 34 [-5.66, 68.33] 40.0 [-6.19, 74.85] 30 [15.52, 39.15] 30.0 [1.04, 48.29]

答案2

得分: 0

This code does the job:

  1. library(dplyr)
  2. dfci <- df %>%
  3. group_by(group) %>%
  4. summarise(across(everything(), list(
  5. mean = ~ mean(., na.rm = TRUE, trim = 4),
  6. ci = ~ { # 自定义的 CI 函数
  7. se <- sqrt(var(., na.rm = TRUE) / sum(!is.na(.)))
  8. mean_val <- mean(., na.rm = TRUE)
  9. lower <- mean_val - qt(0.975, df = sum(!is.na(.))) * se
  10. upper <- mean_val + qt(0.975, df = sum(!is.na(.))) * se
  11. paste0("[", round(lower, 2), ", ", round(upper, 2), "]")
  12. }
  13. ), .names = "{.col}_{.fn}")) %>%
  14. ungroup()
英文:

Unfortunately the earlier answers did not work as they repeated the same ci throughout.

This code does the job:

  1. library(dplyr)
  2. dfci &lt;- df %&gt;%
  3. group_by(group) %&gt;%
  4. summarise(across(everything(), list(
  5. mean = ~ mean(., na.rm = TRUE, trim = 4),
  6. ci = ~ { # OWN CI FUNCTION
  7. se &lt;- sqrt(var(., na.rm = TRUE) / sum(!is.na(.)))
  8. mean_val &lt;- mean(., na.rm = TRUE)
  9. lower &lt;- mean_val - qt(0.975, df = sum(!is.na(.))) * se
  10. upper &lt;- mean_val + qt(0.975, df = sum(!is.na(.))) * se
  11. paste0(&quot;[&quot;, round(lower, 2), &quot;, &quot;, round(upper, 2), &quot;]&quot;)
  12. }
  13. ), .names = &quot;{.col}_{.fn}&quot;)) %&gt;%
  14. ungroup()

huangapple
  • 本文由 发表于 2023年5月18日 01:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定