Calculating length of 95%-CI using dplyr

前端 未结 3 906
北荒
北荒 2020-12-28 22:00

Last time I asked how it was possible to calculate the average score per measurement occasion (week) for a variable (procras) that has been measured repeatedly for multiple

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 22:43

    You could do it manually using mutate a few extra functions in summarise

    library(dplyr)
    mtcars %>%
      group_by(vs) %>%
      summarise(mean.mpg = mean(mpg, na.rm = TRUE),
                sd.mpg = sd(mpg, na.rm = TRUE),
                n.mpg = n()) %>%
      mutate(se.mpg = sd.mpg / sqrt(n.mpg),
             lower.ci.mpg = mean.mpg - qt(1 - (0.05 / 2), n.mpg - 1) * se.mpg,
             upper.ci.mpg = mean.mpg + qt(1 - (0.05 / 2), n.mpg - 1) * se.mpg)
    
    #> Source: local data frame [2 x 7]
    #> 
    #>      vs mean.mpg   sd.mpg n.mpg    se.mpg lower.ci.mpg upper.ci.mpg
    #>   (dbl)    (dbl)    (dbl) (int)     (dbl)        (dbl)        (dbl)
    #> 1     0 16.61667 3.860699    18 0.9099756     14.69679     18.53655
    #> 2     1 24.55714 5.378978    14 1.4375924     21.45141     27.66287
    

提交回复
热议问题