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

remove rounding off errors #24

Merged
merged 7 commits into from
Feb 13, 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
6 changes: 2 additions & 4 deletions R/infer-levene-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
#' @references
#' {Bland, M. 2000. An Introduction to Medical Statistics. 3rd ed. Oxford: Oxford University Press.}
#'
#' {Brown, M. B., and A. B. Forsythe. 1974. Robust tests for the equality of variances. Journal of the American Statistical

#' {Brown, M. B., and A. B. Forsythe. 1974. Robust tests for the equality of variances. Journal of the American Statistical Association 69: 364–367.}
#'


#' {Carroll, R. J., and H. Schneider. 1985. A note on Levene’s tests for equality of variances. Statistics and Probability Letters 3: 191–194.}
#' @examples
#' # using grouping variable
#' infer_levene_test(hsb, read, group_var = race)
Expand Down
16 changes: 8 additions & 8 deletions R/infer-os-t-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @param x numeric; column in \code{data}
#' @param mu a number indicating the true value of the mean
#' @param alpha acceptable tolerance for type I error
#' @param type a character string specifying the alternative hypothesis, must be
#' @param alternative a character string specifying the alternative hypothesis, must be
#' one of "both" (default), "greater", "less" or "all". You can specify just the
#' initial letter
#' @param ... additional arguments passed to or from other methods
Expand Down Expand Up @@ -38,25 +38,25 @@
#'
#' @examples
#' # lower tail
#' infer_os_t_test(hsb, write, mu = 50, type = 'less')
#' infer_os_t_test(hsb, write, mu = 50, alternative = 'less')
#'
#' # upper tail
#' infer_os_t_test(hsb, write, mu = 50, type = 'greater')
#' infer_os_t_test(hsb, write, mu = 50, alternative = 'greater')
#'
#' # both tails
#' infer_os_t_test(hsb, write, mu = 50, type = 'both')
#' infer_os_t_test(hsb, write, mu = 50, alternative = 'both')
#'
#' # all tails
#' infer_os_t_test(hsb, write, mu = 50, type = 'all')
#' infer_os_t_test(hsb, write, mu = 50, alternative = 'all')
#' @export
#'
infer_os_t_test <- function(data, x, mu = 0, alpha = 0.05,
type = c("both", "less", "greater", "all"), ...) UseMethod("infer_os_t_test")
alternative = c("both", "less", "greater", "all"), ...) UseMethod("infer_os_t_test")

#' @export
#'
infer_os_t_test.default <- function(data, x, mu = 0, alpha = 0.05,
type = c("both", "less", "greater", "all"), ...) {
alternative = c("both", "less", "greater", "all"), ...) {
x1 <- enquo(x)

xone <-
Expand All @@ -73,7 +73,7 @@ infer_os_t_test.default <- function(data, x, mu = 0, alpha = 0.05,
stop("alpha must be numeric")
}

type <- match.arg(type)
type <- match.arg(alternative)

var_name <-
data %>%
Expand Down
5 changes: 3 additions & 2 deletions R/infer-os-var-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ infer_os_var_test.default <- function(data, x, sd, confint = 0.95,
k <- osvar_comp(xone, sd, confint)

result <- list(
n = k$n, sd = k$sd, sigma = k$sigma, se = k$se, chi = k$chi,
n = k$n, sd = k$sd, sigma = round(k$sigma, 4), se = round(k$se, 4),
chi = round(k$chi, 4),
df = k$df, p_lower = k$p_lower, p_upper = k$p_upper, p_two = k$p_two,
xbar = k$xbar, c_lwr = k$c_lwr, c_upr = k$c_upr, var_name = varname,
xbar = round(k$xbar, 4), c_lwr = k$c_lwr, c_upr = k$c_upr, var_name = varname,
conf = k$conf, type = type
)

Expand Down
18 changes: 10 additions & 8 deletions R/infer-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -703,34 +703,36 @@ print_prop_test <- function(data) {
}

print_ts_prop_test <- function(data) {
cwidth <- max(nchar("z"), nchar("Pr(|Z| > |z|)"), nchar("Sample Size"))
nwidth <- max(nchar(data$z), nchar(data$sig[1]), nchar(data$n))
cwidth <- max(nchar("z"), nchar("Pr(|Z| > |z|)"), nchar("Total Observations"))
nwidth <- max(nchar(data$z), nchar(data$sig[1]), nchar(data$n1), nchar(data$n2))
w1 <- sum(cwidth, nwidth, 6)

totobs <- sum(data$n1, data$n2)

if (data$alt == "less") {
cat(format("Test Statistics", width = w1, justify = "centre"), "\n")
cat(rep("-", w1), sep = "", "\n")
cat(format("Sample Size", width = cwidth, justify = "left"), formats(), format(data$n1, width = nwidth, justify = "right"), "\n")
cat(format("Total Observations", width = cwidth, justify = "left"), formats(), format(totobs, width = nwidth, justify = "right"), "\n")
cat(format("z", width = cwidth, justify = "left"), formats(), format(data$z, width = nwidth, justify = "right"), "\n")
cat(format("Pr(Z < z)", width = cwidth, justify = "left"), formats(), format(data$sig, width = nwidth, justify = "right"), "\n\n")
} else if (data$alt == "greater") {
cat(format("Test Statistics", width = w1, justify = "centre"), "\n")
cat(rep("-", w1), sep = "", "\n")
cat(format("Sample Size", width = cwidth, justify = "left"), formats(), format(data$n1, width = nwidth, justify = "right"), "\n")
cat(format("Total Observations", width = cwidth, justify = "left"), formats(), format(totobs, width = nwidth, justify = "right"), "\n")
cat(format("z", width = cwidth, justify = "left"), formats(), format(data$z, width = nwidth, justify = "right"), "\n")
cat(format("Pr(Z > z)", width = cwidth, justify = "left"), formats(), format(data$sig, width = nwidth, justify = "right"), "\n\n")
} else if (data$alt == "both") {
cat(format("Test Statistics", width = w1, justify = "centre"), "\n")
cat(rep("-", w1), sep = "", "\n")
cat(format("Sample Size", width = cwidth, justify = "left"), formats(), format(data$n1, width = nwidth, justify = "right"), "\n")
cat(format("Total Observations", width = cwidth, justify = "left"), formats(), format(totobs, width = nwidth, justify = "right"), "\n")
cat(format("z", width = cwidth, justify = "left"), formats(), format(data$z, width = nwidth, justify = "right"), "\n")
cat(format("Pr(|Z| > |z|)", width = cwidth, justify = "left"), formats(), format(data$sig, width = nwidth, justify = "right"), "\n\n")
cat(format("Pr(|Z| < |z|)", width = cwidth, justify = "left"), formats(), format(data$sig, width = nwidth, justify = "right"), "\n\n")
} else {
cat(format("Test Statistics", width = w1, justify = "centre"), "\n")
cat(rep("-", w1), sep = "", "\n")
cat(format("Sample Size", width = cwidth, justify = "left"), formats(), format(data$n1, width = nwidth, justify = "right"), "\n")
cat(format("Total Observations", width = cwidth, justify = "left"), formats(), format(totobs, width = nwidth, justify = "right"), "\n")
cat(format("z", width = cwidth, justify = "left"), formats(), format(data$z, width = nwidth, justify = "right"), "\n")
cat(format("Pr(|Z| > |z|)", width = cwidth, justify = "left"), formats(), format(unname(data$sig[1]), width = nwidth, justify = "right"), "\n")
cat(format("Pr(|Z| < |z|)", width = cwidth, justify = "left"), formats(), format(unname(data$sig[1]), width = nwidth, justify = "right"), "\n")
cat(format("Pr(Z < z)", width = cwidth, justify = "left"), formats(), format(unname(data$sig[2]), width = nwidth, justify = "right"), "\n")
cat(format("Pr(Z > z)", width = cwidth, justify = "left"), formats(), format(unname(data$sig[3]), width = nwidth, justify = "right"), "\n\n")
}
Expand Down
12 changes: 5 additions & 7 deletions R/infer-runs-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@
#' \item{p}{p-value of \code{z}}
#' @section Deprecated Function:
#' \code{runs_test()} has been deprecated. Instead use \code{infer_runs_test()}.
#' @references Sheskin, D. J. 2007. Handbook of Parametric and Nonparametric
#' Statistical Procedures, 4th edition. : Chapman & Hall/CRC.
#' @references
#' {Sheskin, D. J. 2007. Handbook of Parametric and Nonparametric Statistical Procedures, 4th edition. : Chapman & Hall/CRC.}
#'
#' Edgington, E. S. 1961. Probability table for number of runs of signs of first differences in ordered series. Journal of

#' {Edgington, E. S. 1961. Probability table for number of runs of signs of first differences in ordered series. Journal of the American Statistical Association 56: 156–159.}
#'
#' Madansky, A. 1988. Prescriptions for Working Statisticians. New York: Springer.
#' {Madansky, A. 1988. Prescriptions for Working Statisticians. New York: Springer.}
#'
#' Swed, F. S., and C. Eisenhart. 1943. Tables for testing randomness of grouping in a sequence of alternatives. Annals

#' {Swed, F. S., and C. Eisenhart. 1943. Tables for testing randomness of grouping in a sequence of alternatives. Annals of Mathematical Statistics 14: 66–87.}
#' @examples
#' infer_runs_test(hsb, read)
#'
Expand Down
8 changes: 5 additions & 3 deletions R/infer-ts-ind-ttest.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ infer_ts_ind_ttest.default <- function(data, x, y, confint = 0.95,
levels = g_stat[, 1], obs = g_stat[, 2], n = k$n,
mean = g_stat[, 3], sd = g_stat[, 4], se = g_stat[, 5],
lower = g_stat[, 8], upper = g_stat[, 9], combined = comb,
mean_diff = k$mean_diff, sd_dif = k$sd_dif, se_dif = k$se_dif,
conf_diff = k$conf_diff, df_pooled = m$df_pooled,
df_satterthwaite = j$d_f, t_pooled = m$t_pooled, t_satterthwaite = j$t,
mean_diff = round(k$mean_diff, 3), sd_dif = round(k$sd_dif, 3),
se_dif = round(k$se_dif, 3),
conf_diff = round(k$conf_diff, 5), df_pooled = m$df_pooled,
df_satterthwaite = j$d_f, t_pooled = round(m$t_pooled, 4),
t_satterthwaite = round(j$t, 4),
sig_pooled_l = m$sig_pooled_l, sig_pooled_u = m$sig_pooled_u,
sig_pooled = m$sig_pooled, sig = j$sig, sig_l = j$sig_l, sig_u = j$sig_u,
num_df = k$n1 - 1, den_df = k$n2 - 1, f = round(k$s1 / k$s2, 4),
Expand Down
54 changes: 30 additions & 24 deletions R/infer-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,10 @@ prop_comp <- function(n, prob, alternative, phat) {
osvar_comp <- function(x, sd, confint) {
n <- length(x)
df <- n - 1
xbar <- round(mean(x), 4)
sigma <- round(sd(x), 4)
se <- round(sigma / sqrt(n), 4)
chi <- round((df * (sigma / sd) ^ 2), 4)
xbar <- mean(x)
sigma <- sd(x)
se <- sigma / sqrt(n)
chi <- df * ((sigma / sd) ^ 2)

p_lower <- pchisq(chi, df)
p_upper <- pchisq(chi, df, lower.tail = F)
Expand Down Expand Up @@ -672,9 +672,9 @@ indth <- function(data, x, y, a) {

h <- data_split(data, !! x1, !! y1)
h$df <- h$length - 1
h$error <- round(qt(a, h$df), 3) * -1
h$lower <- round(h$mean_t - (h$error * h$std_err), 3)
h$upper <- round(h$mean_t + (h$error * h$std_err), 3)
h$error <- qt(a, h$df) * -1
h$lower <- h$mean_t - (h$error * h$std_err)
h$upper <- h$mean_t + (h$error * h$std_err)
return(h)
}

Expand All @@ -683,9 +683,9 @@ indcomb <- function(data, y, a) {

comb <- da(data, !! y1)
comb$df <- comb$length - 1
comb$error <- round(qt(a, comb$df), 3) * -1
comb$lower <- round(comb$mean_t - (comb$error * comb$std_err), 3)
comb$upper <- round(comb$mean_t + (comb$error * comb$std_err), 3)
comb$error <- qt(a, comb$df) * -1
comb$lower <- round(comb$mean_t - (comb$error * comb$std_err), 5)
comb$upper <- round(comb$mean_t + (comb$error * comb$std_err), 5)
names(comb) <- NULL
return(comb)
}
Expand All @@ -696,13 +696,13 @@ indcomp <- function(grp_stat, alpha) {
n <- n1 + n2
means <- grp_stat[, 3]
mean_diff <- means[1] - means[2]
sd1 <- round(grp_stat[1, 4], 3)
sd2 <- round(grp_stat[2, 4], 3)
s1 <- round(grp_stat[1, 4] ^ 2, 3)
s2 <- round(grp_stat[2, 4] ^ 2, 3)
sd_dif <- round(sd_diff(n1, n2, s1, s2), 3)
se_dif <- round(se_diff(n1, n2, s1, s2), 3)
conf_diff <- round(conf_int_p(mean_diff, se_dif, alpha = alpha), 3)
sd1 <- grp_stat[1, 4]
sd2 <- grp_stat[2, 4]
s1 <- grp_stat[1, 4] ^ 2
s2 <- grp_stat[2, 4] ^ 2
sd_dif <- sd_diff(n1, n2, s1, s2)
se_dif <- se_diff(n1, n2, s1, s2)
conf_diff <- conf_int_p(mean_diff, se_dif, alpha = alpha)
out <- list(
n1 = n1, n2 = n2, n = n, mean_diff = mean_diff, sd1 = sd1,
sd2 = sd2, s1 = s1, s2 = s2, sd_dif = sd_dif, se_dif = se_dif,
Expand All @@ -713,7 +713,7 @@ indcomp <- function(grp_stat, alpha) {

indsig <- function(n1, n2, s1, s2, mean_diff) {
d_f <- as.vector(df(n1, n2, s1, s2))
t <- round(mean_diff / (((s1 / n1) + (s2 / n2)) ^ 0.5), 4)
t <- mean_diff / (((s1 / n1) + (s2 / n2)) ^ 0.5)
sig_l <- round(pt(t, d_f), 4)
sig_u <- round(pt(t, d_f, lower.tail = FALSE), 4)
if (sig_l < 0.5) {
Expand All @@ -727,9 +727,8 @@ indsig <- function(n1, n2, s1, s2, mean_diff) {

fsig <- function(s1, s2, n1, n2) {
out <- round(min(
pf(round(s1 / s2, 4), (n1 - 1), (n2 - 1)),
pf(
round(s1 / s2, 4), (n1 - 1), (n2 - 1),
pf((s1 / s2), (n1 - 1), (n2 - 1)),
pf((s1 / s2), (n1 - 1), (n2 - 1),
lower.tail = FALSE
)
) * 2, 4)
Expand All @@ -739,7 +738,7 @@ fsig <- function(s1, s2, n1, n2) {

indpool <- function(n1, n2, mean_diff, se_dif) {
df_pooled <- (n1 + n2) - 2
t_pooled <- round(mean_diff / se_dif, 4)
t_pooled <- mean_diff / se_dif
sig_pooled_l <- round(pt(t_pooled, df_pooled), 4)
sig_pooled_u <- round(pt(t_pooled, df_pooled, lower.tail = FALSE), 4)
if (sig_pooled_l < 0.5) {
Expand All @@ -757,6 +756,7 @@ indpool <- function(n1, n2, mean_diff, se_dif) {

#' @importFrom rlang sym
tibble_stats <- function(data, x, y) {

by_factor <- data %>%
group_by(!! sym(y)) %>%
select(!! sym(y), !! sym(x)) %>%
Expand All @@ -765,18 +765,23 @@ tibble_stats <- function(data, x, y) {
mutate(
ses = sd / sqrt(length)
)

return(by_factor)

}

tbl_stats <- function(data, y) {

avg <- data %>%
select(y) %>%
summarise_all(funs(length, mean, sd)) %>%
as_data_frame() %>%
mutate(
se = sd / sqrt(length)
)

return(unlist(avg, use.names = FALSE))

}


Expand Down Expand Up @@ -852,6 +857,7 @@ paired_data <- function(x, y) {

#' @importFrom dplyr select
paired_stats <- function(data, key, value) {

d <- data %>%
group_by(key) %>%
select(value, key) %>%
Expand All @@ -860,8 +866,8 @@ paired_stats <- function(data, key, value) {
mutate(
se = sd / sqrt(length)
) %>%
select(-(key:length)) %>%
round(2)
select(-(key:length))

return(d)
}

Expand Down
4 changes: 1 addition & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ inferr: Inferential statistics with R
**Author:** [Aravind Hebbali](http://www.aravindhebbali.com)<br/>
**License:** [MIT](https://opensource.org/licenses/MIT)

[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/inferr)](https://cran.r-project.org/package=inferr) [![Travis-CI Build Status](https://travis-ci.org/rsquaredacademy/inferr.svg?branch=master)](https://travis-ci.org/rsquaredacademy/inferr) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/rsquaredacademy/inferr?branch=master&svg=true)](https://ci.appveyor.com/project/rsquaredacademy/inferr) [![](https://cranlogs.r-pkg.org/badges/grand-total/inferr)](https://cran.r-project.org/package=inferr) [![Coverage status](https://codecov.io/gh/rsquaredacademy/inferr/branch/master/graph/badge.svg)](https://codecov.io/github/rsquaredacademy/inferr?branch=master) ![](https://img.shields.io/badge/lifecycle-maturing-blue.svg)


[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/inferr)](https://cran.r-project.org/package=inferr) [![Travis-CI Build Status](https://travis-ci.org/rsquaredacademy/inferr.svg?branch=master)](https://travis-ci.org/rsquaredacademy/inferr) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/rsquaredacademy/inferr?branch=master&svg=true)](https://ci.appveyor.com/project/rsquaredacademy/inferr) [![](https://cranlogs.r-pkg.org/badges/grand-total/inferr)](https://cran.r-project.org/package=inferr)

## Overview

Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ Status](https://travis-ci.org/rsquaredacademy/inferr.svg?branch=master)](https:/
[![AppVeyor Build
Status](https://ci.appveyor.com/api/projects/status/github/rsquaredacademy/inferr?branch=master&svg=true)](https://ci.appveyor.com/project/rsquaredacademy/inferr)
[![](https://cranlogs.r-pkg.org/badges/grand-total/inferr)](https://cran.r-project.org/package=inferr)
[![Coverage
status](https://codecov.io/gh/rsquaredacademy/inferr/branch/master/graph/badge.svg)](https://codecov.io/github/rsquaredacademy/inferr?branch=master)
![](https://img.shields.io/badge/lifecycle-maturing-blue.svg)

## Overview

Expand Down
2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading