library(dplyr)
library(broom)
tidyx <- tidy(x)
onesamp <- TRUE  
if (length(x$estimate) == 2) onesamp <- FALSE
stmt1 <- 
    if (onesamp) {
        paste0(
            "This was a one-sample proportion test of the null hypothesis that the true population proportion is equal to ",
            x$null.value
        )
    } else {
        "This was a two-sample proportion test of the null hypothesis that the true population proportions are equal"
    }
stmt2 <- 
    if (x$p.value < 1 - attr(x$conf.int, "conf")) {
        if (onesamp) {
            paste0(
                "conclude that true population proportion is different than ",
                x$null.value
            ) 
        } else {
            "conclude that two population proportions are not equal"
        }

    } else {
        if (onesamp) {
            paste0(
                "cannot conclude that true population proportion is different than ",
                x$null.value
            ) 
        } else {
            "cannot conclude that two population proportions are different from one another"
        }
    }
## TODO: Add logic to take out sample size numbers, in the case that you've inputted a variable rather than a raw value
stmt3 <- 

    if (onesamp) {

        samps <- strsplit(x$data.name, ",") %>%
            unlist() %>%
            strsplit(split = " ") %>%
            unlist()

        x1 <- as.numeric(samps[1])
        n1 <- as.numeric(samps[4])

        paste0(
            "The observed sample proportion is ",
            x$estimate,
            " (",
            prettyNum(x1, big.mark=","),
            " events out of a total sample size of ",
            prettyNum(n1, big.mark=","),
            ")"
        )    
    } else {

        samps <- strsplit(x$data.name, "\\(" ) %>%
            unlist() %>%
            strsplit(split="\\)") %>%
            unlist() %>%
            strsplit(split=",") %>%
            unlist()

        x1 <- as.numeric(samps[2])
        x2 <- as.numeric(samps[3])
        n1 <- as.numeric(samps[5])
        n2 <- as.numeric(samps[6])  

        paste0(
            "The observed difference in proportions is ",
            x$estimate[2] - x$estimate[1],
            ". The observed proportion for the first group is ",
            x$estimate[1],
            " (",
            prettyNum(x1, big.mark=","), 
            " events out of a total sample size of ",
            prettyNum(n1, big.mark=","), 
            "). For the second group, the observed proportion is ",
            x$estimate[2],
            " (",
            prettyNum(x2, big.mark=","), 
            ", out of a total sample size of ",
            prettyNum(n2, big.mark=","),
            ")"
        )
    }
stmt4 <- 

    if(onesamp) {

        paste0(
            x$null.value + abs(x$estimate - x$null.value), 
            " or less than ",
            x$null.value - abs(x$estimate - x$null.value) 
        )

    } else {

        paste0(
            abs(x$estimate[2] - x$estimate[1]),
            " or less than ",
            -abs(x$estimate[2] - x$estimate[1])
        )
    }

r stmt1. Using a significance level of r 1 - attr(x$conf.int, "conf"), we r if (x$p.value < 1 - attr(x$conf.int, "conf")) "reject" else "do not reject" the null hypothesis, and r stmt2. r stmt3.

The confidence interval for the true r if (onesamp) "population proportion" else "difference in population proportions" is (r tidyx$conf.low, r tidyx$conf.high). This interval will contain the true r if(onesamp) "population proportion" else "difference in population proportions" 95 times out of 100.

The p-value for this test is r x$p.value. This, formally, is defined as the probability -- if the null hypothesis is true -- of observing a r if (onesamp) "sample proportion" else "difference in sample proportions" that is as or more extreme than the r if (onesamp) "sample proportion" else "difference in sample proportions" from this data set. In this case, this is the probability -- if the true r if (onesamp) paste0("population proportion is ", x$null.value) else "population proportions are equal" -- of observing a r if (onesamp) "sample proportion" else "difference in sample proportions" that is greater than r stmt4.



hilaryparker/explainr documentation built on May 17, 2019, 3:58 p.m.