ci_commons.R 5.45 KB
Newer Older
Holger Brandl's avatar
Holger Brandl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

# requires
# devtools::source_url("https://raw.githubusercontent.com/holgerbrandl/datautils/v1.43/R/core_commons.R")

calc_ci = function(df, variable, ci_interval=0.95){
    variable <- enquo(variable)

    # http://dplyr.tidyverse.org/articles/programming.html
    mean_name <- paste0( quo_name(variable), "_mean")
    ci_name <- paste0(quo_name(variable), "_ci")
    # echo(glue::glue("varname is {ci_name}"))

    df %>% summarize(
    mean=mean(!!variable),
    sd=sd(!!variable),
    N = n(),
    se=sd/sqrt(N),
    !!ci_name := qt(ci_interval/2+0.5, N-1)*se,
    !!mean_name := mean
    ) %>% select(-c(mean, sd, N, se, mean))
}

iris %>% group_by(Species) %>% calc_ci(Sepal.Length)
iris %>% group_by(Species, Sepal.Width>3) %>% calc_ci(Sepal.Length)


#' now with plotting
plot_ci = function(grpData, variable, ci_interval=0.95){
    variable <- enquo(variable)

Holger Brandl's avatar
Holger Brandl committed
31 32
    # browser()

Holger Brandl's avatar
Holger Brandl committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    # calculte ci intervals
    ciData = grpData %>% summarize(
        mean=mean(!!variable),
        sd=sd(!!variable),
        N = n(),
        se=sd/sqrt(N),
        ci = qt(ci_interval/2+0.5, N-1)*se,
    )


    #fail if there are more than one group attributes
    assert(length(groups(grpData)) < 3, "more than 2 groups are not supported")

    groupVar1 = groups(grpData)[[1]]
    gg = ggplot(grpData, aes(x= eval(rlang::UQE(groupVar1)), y= eval(rlang::UQE(variable)))) +
Holger Brandl's avatar
Holger Brandl committed
48
        geom_jitter(alpha=0.3, height=0,width=0.2) +
Holger Brandl's avatar
Holger Brandl committed
49 50
        geom_errorbar(aes(ymin= mean-ci, ymax= mean+ci, y=NULL), data=ciData, width=.2, size=0.9)

Holger Brandl's avatar
Holger Brandl committed
51 52
    gg = gg + xlab(groupVar1) + ylab(quo_name(variable))

Holger Brandl's avatar
Holger Brandl committed
53 54 55 56 57 58
    # if 2 grouping variables are present add facetting on second grouping attribute
    if(length(groups(grpData)) ==2){
        # https://stackoverflow.com/questions/21588096/pass-string-to-facet-grid-ggplot2
        gg =  gg + facet_wrap(as.formula(paste("~",groups(grpData)[[2]])))
    }

Holger Brandl's avatar
Holger Brandl committed
59
    gg
Holger Brandl's avatar
Holger Brandl committed
60 61 62
}


Holger Brandl's avatar
Holger Brandl committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
## todo support model formula instead of group data
plot_interaction = function(grpData, variable, ci_interval=0.95){
    variable <- enquo(variable)

    #fail if there are more not 2 group attributes
    assert(length(groups(grpData)) == 2, "only dfs with 2 grouping vars are supported")

    # calculte ci intervals
    ciData = grpData %>% summarize(
        mean=mean(!!variable),
        sd=sd(!!variable),
        N = n(),
        se=sd/sqrt(N),
        ci = qt(ci_interval/2+0.5, N-1)*se,
    )


    groupVar1 = groups(grpData)[[1]]
    groupVar2 = groups(grpData)[[2]]
    dodge_with=0.2

    gg = ggplot(grpData, aes(x = eval(rlang::UQE(groupVar1)), y = eval(rlang::UQE(variable)), color = eval(rlang::UQE(groupVar2)))) +
        geom_jitter(position = position_jitterdodge(jitter.width = 0.1, dodge.width = dodge_with), alpha = 0.3) +
        geom_errorbar(aes(ymin = mean - ci, ymax = mean + ci, y = NULL), data = ciData, width = .2, size = 0.9, position = position_dodge(width = dodge_with)) +
        # geom_line(aes(y = mean, group = eval(rlang::UQE(groupVar2))), position = position_dodge(width = dodge_with), data = ciData) +
        xlab(groupVar1) +
        ylab(quo_name(variable))

    gg
}

## DEBUG-START
if(F){
    devtools::source_url("https://raw.githubusercontent.com/holgerbrandl/datautils/v1.43/R/ggplot_commons.R")

    lmModel = lm(len ~ supp*dose, data = ToothGrowth)
    str(lmModel)

    ToothGrowth %>% group_by(supp, as.factor(dose)) %>% plot_confint(len)
    ToothGrowth %>% group_by(supp, dose) %>% plot_ci(len)
    # ToothGrowth %>% mutate_inplace(dose, as.factor()) %>% group_by(supp, dose) %>% plot_ci(len)

    .plot_confint = function(grpData, variable, ci_interval=0.95) plot_confint(grpData, quo_name(variable), ci_interval)

    interaction_plot = function(grpData, variable, ci_interval=0.95){
        ## invert the grouping
        grpData = ToothGrowth %>% group_by(supp, as.factor(dose))
        groupVar1 = groups(grpData)[[1]]
        groupVar2 = groups(grpData)[[2]]
        browser()
        regrouped = grpData %>% group_by(eval(rlang::UQE(groupVar2)), eval(rlang::UQE(groupVar1)))

        multiplot(
            .plot_confint(grpData, rlang::UQ(variable)),
            plot_confint(grpData, enquo(variable)),
            plot_confint(regrouped, enquo(variable))
        )
    }

ToothGrowth %>% group_by(supp, as.factor(dose))  %>% interaction_plot(len)

}
## DEBUG-END


Holger Brandl's avatar
Holger Brandl committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
########################################################################################################################
### DEV PLAYGROUND

if(F){

    # iris %>% ggplot(aes(Sepal.Width)) + geom_histogram()
    iris %>%
        group_by(Species) %>%
        plot_ci(Petal.Length)
    iris %>%
        group_by(Sepal.Width > 3, Species) %>%
        plot_ci(Petal.Length)
    iris %>%
        group_by(Sepal.Width > 3, Sepal.Width > 4, Species) %>%
        plot_ci(Petal.Length)



    ## see https://stackoverflow.com/questions/43405843/how-to-use-the-devel-version-of-dplyrs-enquo-and-quo-name-in-a-function-with-ti/43601059
    ## also see https://stackoverflow.com/questions/45279287/use-dplyr-se-with-ggplot2

    #' generic simple examples
    some_plot = function(data, var){
        variable <- enquo(var)
        tt = quo_name(variable)
        ggplot(data, aes_string(tt)) + geom_histogram()
    }
    some_plot(iris, Sepal.Length) -> works

    ## without using aes_string
    some_plot = function(data, var){
        variable <- enquo(var)
        # ggplot(data, aes(eval(rlang::UQE(variable))+eval(rlang::UQE(variable)))) + geom_histogram()
        ggplot(data, aes(eval(rlang::UQE(variable)))) + geom_histogram()
    }
    some_plot(iris, Petal.Length)

}