Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup code #33

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions R/infer-anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ infer_oneway_anova <- function(data, x, y, ...) UseMethod("infer_oneway_anova")

#' @export
infer_oneway_anova.default <- function(data, x, y, ...) {

x1 <- enquo(x)
y1 <- enquo(y)

fdata <-
data %>%
select(!! x1, !! y1)

sample_mean <- anova_avg(fdata, !! x1)
fdata <- select(data, !! x1, !! y1)
sample_mean <- anova_avg(fdata, !! x1)
sample_stats <- anova_split(fdata, !! x1, !! y1, sample_mean)
k <- anova_calc(fdata, sample_stats, !! x1, !! y1)
k <- anova_calc(fdata, sample_stats, !! x1, !! y1)


result <- list(
Expand Down
6 changes: 2 additions & 4 deletions R/infer-binom-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ print.infer_binom_calc <- function(x, ...) {
#' @export
#' @rdname infer_binom_calc
infer_binom_test <- function(data, variable, prob = 0.5) {

varyable <- enquo(variable)

fdata <-
data %>%
pull(!! varyable)
fdata <- pull(data, !! varyable)

if (!is.factor(fdata)) {
stop("variable must be of type factor", call. = FALSE)
Expand Down
24 changes: 10 additions & 14 deletions R/infer-chisq-assoc-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@ infer_chisq_assoc_test <- function(data, x, y) UseMethod("infer_chisq_assoc_test

#' @export
infer_chisq_assoc_test.default <- function(data, x, y) {

x1 <- enquo(x)
y1 <- enquo(y)

xone <-
data %>%
pull(!! x1)

yone <-
data %>%
pull(!! y1)
xone <- pull(data, !! x1)
yone <- pull(data, !! y1)

if (!is.factor(xone)) {
stop("x must be a categorical variable")
Expand All @@ -61,7 +57,7 @@ infer_chisq_assoc_test.default <- function(data, x, y) {
}

# dimensions
k <- table(xone, yone)
k <- table(xone, yone)
dk <- dim(k)
ds <- prod(dk)
nr <- dk[1]
Expand All @@ -72,16 +68,16 @@ infer_chisq_assoc_test.default <- function(data, x, y) {
twoway <- matrix(table(xone, yone), nrow = 2)
df <- df_chi(twoway)
ef <- efmat(twoway)
k <- pear_chsq(twoway, df, ef)
m <- lr_chsq(twoway, df, ef)
n <- yates_chsq(twoway)
p <- mh_chsq(twoway, n$total, n$prod_totals)
k <- pear_chsq(twoway, df, ef)
m <- lr_chsq(twoway, df, ef)
n <- yates_chsq(twoway)
p <- mh_chsq(twoway, n$total, n$prod_totals)
} else {
twoway <- matrix(table(xone, yone), nrow = dk[1])
ef <- efm(twoway, dk)
df <- df_chi(twoway)
k <- pear_chi(twoway, df, ef)
m <- lr_chsq2(twoway, df, ef, ds)
k <- pear_chi(twoway, df, ef)
m <- lr_chsq2(twoway, df, ef, ds)
}

j <- chigf(xone, yone, k$chi)
Expand Down
6 changes: 2 additions & 4 deletions R/infer-chisq-gof-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ infer_chisq_gof_test <- function(data, x, y, correct = FALSE) UseMethod("infer_c

#' @export
infer_chisq_gof_test.default <- function(data, x, y, correct = FALSE) {
x1 <- enquo(x)

xcheck <-
data %>%
pull(!! x1)
x1 <- enquo(x)
xcheck <- pull(data, !! x1)

xlen <-
data %>%
Expand Down
4 changes: 2 additions & 2 deletions R/infer-cochran-q-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ infer_cochran_qtest <- function(data, ...) UseMethod("infer_cochran_qtest")

#' @export
infer_cochran_qtest.default <- function(data, ...) {

vars <- quos(...)

fdata <- data %>%
select(!!! vars)
fdata <- select(data, !!! vars)

if (ncol(fdata) < 3) {
stop("Please specify at least 3 variables.")
Expand Down
22 changes: 7 additions & 15 deletions R/infer-levene-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,29 @@ infer_levene_test <- function(data, ...) UseMethod("infer_levene_test")
#' @rdname infer_levene_test
infer_levene_test.default <- function(data, ..., group_var = NULL,
trim_mean = 0.1) {
groupvar <- enquo(group_var)


groupvar <- enquo(group_var)
varyables <- quos(...)

fdata <-
data %>%
select(!!! varyables)
fdata <- select(data, !!! varyables)

if (quo_is_null(groupvar)) {
z <- as.list(fdata)
z <- as.list(fdata)
ln <- z %>% map_int(length)
ly <- seq_len(length(z))

if (length(z) < 2) {
stop("Please specify at least two variables.", call. = FALSE)
}

out <- gvar(ln, ly)
out <- gvar(ln, ly)
fdata <- unlist(z)
groupvars <-
out %>%
unlist() %>%
as.factor()
} else {
fdata <-
fdata %>%
pull(1)

groupvars <-
data %>%
pull(!! groupvar)
fdata <- pull(fdata, 1)
groupvars <- pull(data, !! groupvar)

if (length(fdata) != length(groupvars)) {
stop("Length of variable and group_var do not match.", call. = FALSE)
Expand Down
1 change: 1 addition & 0 deletions R/infer-mcnemar-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ infer_mcnemar_test <- function(data, x = NULL, y = NULL) UseMethod("infer_mcnema
#' @export
#'
infer_mcnemar_test.default <- function(data, x = NULL, y = NULL) {

if (is.matrix(data) | is.table(data)) {
dat <- mcdata(data)
} else {
Expand Down
17 changes: 7 additions & 10 deletions R/infer-os-prop-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,26 @@ infer_os_prop_test <- function(data, variable = NULL, prob = 0.5, phat = 0.5,
infer_os_prop_test.default <- function(data, variable = NULL, prob = 0.5, phat = 0.5,
alternative = c("both", "less", "greater", "all")) {
if (is.numeric(data)) {

method <- match.arg(alternative)
k <- prop_comp(
data, prob = prob, phat = phat,
alternative = method
)
} else {
varyables <- enquo(variable)

fdata <-
data %>%
pull(!! varyables)
} else {

n1 <- length(fdata)
varyables <- enquo(variable)
fdata <- pull(data, !! varyables)
n1 <- length(fdata)

n2 <-
fdata %>%
table() %>%
`[[`(2)

phat <- round(n2 / n1, 4)

prob <- prob

phat <- round(n2 / n1, 4)
prob <- prob
method <- match.arg(alternative)

k <- prop_comp(
Expand Down
8 changes: 3 additions & 5 deletions R/infer-os-t-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ infer_os_t_test <- function(data, x, mu = 0, alpha = 0.05,
#'
infer_os_t_test.default <- function(data, x, mu = 0, alpha = 0.05,
alternative = c("both", "less", "greater", "all"), ...) {
x1 <- enquo(x)

xone <-
data %>%
pull(!! x1)

x1 <- enquo(x)
xone <- pull(data, !! x1)

if (!is.numeric(xone)) {
stop("x must be numeric")
Expand Down
8 changes: 3 additions & 5 deletions R/infer-os-var-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,9 @@ infer_os_var_test <- function(data, x, sd, confint = 0.95,
#'
infer_os_var_test.default <- function(data, x, sd, confint = 0.95,
alternative = c("both", "less", "greater", "all"), ...) {
x1 <- enquo(x)

xone <-
data %>%
pull(!! x1)

x1 <- enquo(x)
xone <- pull(data, !! x1)

if (!is.numeric(xone)) {
stop("x must be numeric")
Expand Down
29 changes: 8 additions & 21 deletions R/infer-runs-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ infer_runs_test <- function(data, x, drop = FALSE, split = FALSE, mean = FALSE,
infer_runs_test.default <- function(data, x, drop = FALSE,
split = FALSE, mean = FALSE,
threshold = NA) {
x1 <- enquo(x)

xone <-
data %>%
pull(!! x1)

n <- length(xone)

# if (!(is.numeric(x) || is.integer(x)))
# stop("x must be numeric or integer")

x1 <- enquo(x)
xone <- pull(data, !! x1)
n <- length(xone)

if (is.na(threshold)) {
y <- unique(xone)
Expand All @@ -77,7 +71,6 @@ infer_runs_test.default <- function(data, x, drop = FALSE,
}
}

# compute threshold
if (!(is.na(threshold))) {
thresh <- threshold
} else if (mean == TRUE) {
Expand All @@ -86,7 +79,6 @@ infer_runs_test.default <- function(data, x, drop = FALSE,
thresh <- median(xone, na.rm = TRUE)
}

# drop values equal to threshold if drop == TRUE
if (drop == TRUE) {
xone <- xone[xone != thresh]
}
Expand All @@ -101,20 +93,15 @@ infer_runs_test.default <- function(data, x, drop = FALSE,
unlist(use.names = FALSE)
}

# compute the number of runs
n_runs <- nsignC(x_binary)
n1 <- sum(x_binary)
n0 <- length(x_binary) - n1

# compute expected runs and sd of runs
n_runs <- nsignC(x_binary)
n1 <- sum(x_binary)
n0 <- length(x_binary) - n1
exp_runs <- expruns(n0, n1)
sd_runs <- sdruns(n0, n1)
sd_runs <- sdruns(n0, n1)

# compute the test statistic
test_stat <- (n_runs - exp_runs) / (sd_runs ^ 0.5)
sig <- 2 * (1 - pnorm(abs(test_stat), lower.tail = TRUE))

# result
result <- list(
n = n, threshold = thresh, n_below = n0, n_above = n1,
mean = exp_runs, var = sd_runs, n_runs = n_runs, z = test_stat,
Expand Down
21 changes: 10 additions & 11 deletions R/infer-ts-ind-ttest.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,17 @@ infer_ts_ind_ttest.default <- function(data, x, y, confint = 0.95,
stop("x must be a binary factor variable", call. = FALSE)
}

method <- match.arg(alternative)
var_y <- yone
alpha <- 1 - confint
a <- alpha / 2

h <- indth(data, !! x1, !! y1, a)
method <- match.arg(alternative)
var_y <- yone
alpha <- 1 - confint
a <- alpha / 2
h <- indth(data, !! x1, !! y1, a)
grp_stat <- h
g_stat <- as.matrix(h)
comb <- indcomb(data, !! y1, a)
k <- indcomp(grp_stat, alpha)
j <- indsig(k$n1, k$n2, k$s1, k$s2, k$mean_diff)
m <- indpool(k$n1, k$n2, k$mean_diff, k$se_dif)
g_stat <- as.matrix(h)
comb <- indcomb(data, !! y1, a)
k <- indcomp(grp_stat, alpha)
j <- indsig(k$n1, k$n2, k$s1, k$s2, k$mean_diff)
m <- indpool(k$n1, k$n2, k$mean_diff, k$se_dif)

result <- list(
levels = g_stat[, 1], obs = g_stat[, 2], n = k$n,
Expand Down
9 changes: 2 additions & 7 deletions R/infer-ts-paired-ttest.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,8 @@ infer_ts_paired_ttest.default <- function(data, x, y, confint = 0.95,
select(!! x1, !! y1) %>%
names()

xone <-
data %>%
pull(!! x1)

yone <-
data %>%
pull(!! y1)
xone <- pull(data, !! x1)
yone <- pull(data, !! y1)

k <- paired_comp(xone, yone, confint, var_names)

Expand Down
Loading