Skip to content

Commit

Permalink
Merge pull request #73 from rsquaredacademy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
aravindhebbali authored May 28, 2021
2 parents 46fe3e6 + 4fc7ae9 commit 7b8a7cc
Show file tree
Hide file tree
Showing 122 changed files with 8,239 additions and 3,195 deletions.
14 changes: 6 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: inferr
Type: Package
Title: Inferential Statistics
Version: 0.3.0.9000
Version: 0.3.1
Authors@R: person("Aravind", "Hebbali", email = "hebbali.aravind@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9220-9669"))
Description: Select set of parametric and non-parametric statistical tests. 'inferr' builds upon the solid set of
Expand All @@ -13,13 +13,11 @@ License: MIT + file LICENSE
URL: https://rsquaredacademy.github.io/inferr/, https://github.com/rsquaredacademy/inferr
BugReports: https://github.com/rsquaredacademy/inferr/issues
Imports:
dplyr,
magrittr,
purrr,
Rcpp,
rlang,
tibble,
tidyr
data.table,
magrittr,
Rcpp,
stats,
utils
Suggests:
covr,
knitr,
Expand Down
51 changes: 8 additions & 43 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ S3method(print,infer_ts_ind_ttest)
S3method(print,infer_ts_paired_ttest)
S3method(print,infer_ts_prop_test)
S3method(print,infer_ts_var_test)
export(binom_calc)
export(binom_test)
export(chisq_gof)
export(chisq_test)
export(cochran_test)
export(ind_ttest)
export(infer_binom_calc)
export(infer_binom_test)
export(infer_chisq_assoc_test)
Expand All @@ -52,51 +46,22 @@ export(infer_runs_test)
export(infer_ts_ind_ttest)
export(infer_ts_paired_ttest)
export(infer_ts_prop_calc)
export(infer_ts_prop_grp)
export(infer_ts_prop_group)
export(infer_ts_prop_test)
export(infer_ts_var_test)
export(levene_test)
export(mcnemar_test)
export(os_vartest)
export(owanova)
export(paired_ttest)
export(prop_test)
export(runs_test)
export(ts_prop_calc)
export(ts_prop_grp)
export(ts_prop_test)
export(ttest)
export(var_test)
import(magrittr)
importFrom(Rcpp,sourceCpp)
importFrom(dplyr,"%>%")
importFrom(dplyr,funs)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,select)
importFrom(dplyr,summarise_all)
importFrom(data.table,":=")
importFrom(data.table,data.table)
importFrom(data.table,setDF)
importFrom(magrittr,"%>%")
importFrom(magrittr,subtract)
importFrom(magrittr,use_series)
importFrom(purrr,map)
importFrom(purrr,map_dbl)
importFrom(purrr,map_df)
importFrom(purrr,map_int)
importFrom(rlang,"!!!")
importFrom(rlang,"!!")
importFrom(rlang,enquo)
importFrom(rlang,quo_is_null)
importFrom(rlang,quos)
importFrom(rlang,sym)
importFrom(stats,anova)
importFrom(stats,as.formula)
importFrom(stats,complete.cases)
importFrom(stats,cor)
importFrom(stats,dbinom)
importFrom(stats,formula)
importFrom(stats,lm)
importFrom(stats,median)
importFrom(stats,model.frame)
importFrom(stats,pbinom)
importFrom(stats,pchisq)
importFrom(stats,pf)
Expand All @@ -107,7 +72,7 @@ importFrom(stats,qnorm)
importFrom(stats,qt)
importFrom(stats,sd)
importFrom(stats,var)
importFrom(tibble,as_data_frame)
importFrom(tibble,tibble)
importFrom(tidyr,gather)
importFrom(utils,install.packages)
importFrom(utils,menu)
importFrom(utils,packageVersion)
useDynLib(inferr)
26 changes: 3 additions & 23 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
# inferr 0.3.0.9000

The following functions were deprecated in `0.3.0` and have been removed:

- `binom_calc`
- `binom_test`
- `chisq_gof`
- `chisq_test`
- `cochran_test`
- `ind_ttest`
- `launch_inferr`
- `levene_test`
- `mcnemar_test`
- `os_vartest`
- `owanova`
- `paired_ttest`
- `prop_test`
- `runs_test`
- `ts_prop_calc`
- `ts_prop_grp`
- `ts_prop_test`
- `ttest`
- `var_test`
# inferr 0.3.0.1

This is a patch release to fix error on R-devel on Debian and Fedora.

# inferr 0.3.0

Expand Down
140 changes: 140 additions & 0 deletions R/ifr-anova.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#' @title One Way ANOVA
#' @description One way analysis of variance
#' @param data a \code{data.frame} or a \code{tibble}
#' @param x numeric; column in \code{data}
#' @param y factor; column in \code{data}
#' @param ... additional arguments passed to or from other methods
#' @return \code{infer_oneway_anova} returns an object of class \code{"infer_oneway_anova"}.
#' An object of class \code{"infer_oneway_anova"} is a list containing the
#' following components:
#'
#' \item{adjusted_r2}{adjusted r squared value}
#' \item{df_btw}{between groups degress of freedom}
#' \item{df_within}{within groups degress of freedom}
#' \item{df_total}{total degress of freedom}
#' \item{fstat}{f value}
#' \item{group_stats}{group statistics}
#' \item{ms_btw}{between groups mean square}
#' \item{ms_within}{within groups mean square}
#' \item{obs}{number of observations}
#' \item{pval}{p value}
#' \item{r2}{r squared value}
#' \item{rmse}{root mean squared error}
#' \item{ss_between}{between group sum of squares}
#' \item{ss_within}{within group sum of squares}
#' \item{ss_total}{total sum of squares}
#'
#' @references Kutner, M. H., Nachtsheim, C., Neter, J., & Li, W. (2005).
#' Applied linear statistical models. Boston: McGraw-Hill Irwin.
#'
#' @seealso \code{\link[stats]{anova}}
#' @examples
#' infer_oneway_anova(mtcars, mpg, cyl)
#' infer_oneway_anova(hsb, write, prog)
#' @export
#'
infer_oneway_anova <- function(data, x, y, ...) UseMethod("infer_oneway_anova")

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

x1 <- deparse(substitute(x))
y1 <- deparse(substitute(y))

fdata <- data[c(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)

result <-
list(
adjusted_r2 = round(k$reg$adj.r.squared, 4),
df_btw = k$df_sstr,
df_total = k$df_sst,
df_within = k$df_sse,
fstat = k$f,
group_stats = sample_stats[, c(1, 2, 3, 5)],
ms_btw = k$mstr,
ms_within = k$mse,
obs = k$obs,
pval = k$sig,
r2 = round(k$reg$r.squared, 4),
rmse = round(k$reg$sigma, 4),
ss_between = k$sstr,
ss_total = k$total,
ss_within = k$ssee)

class(result) <- "infer_oneway_anova"
return(result)
}

#' @export
print.infer_oneway_anova <- function(x, ...) {
print_owanova(x)
}

#' @import magrittr
#' @importFrom stats var sd
#' @importFrom data.table data.table := setDF
anova_split <- function(data, x, y, sample_mean) {

dat <- data[c(y, x)]
dm <- data.table(dat)

by_factor <- dm[, .(length = length(get(x)),
mean = mean(get(x)),
var = var(get(x)),
sd = sd(get(x))),
by = y]

by_factor[, ':='(sst = length * ((mean - sample_mean) ^ 2),
sse = (length - 1) * var)]

setDF(by_factor)
by_factor <- by_factor[order(by_factor[, 1]),]

return(by_factor)
}

anova_avg <- function(data, y) {

mean(data[[y]])

}

#' @importFrom stats pf as.formula lm
anova_calc <- function(data, sample_stats, x, y) {

var_names <- names(data[c(x, y)])

sample_stats %>%
use_series(sst) %>%
sum() %>%
round(3) -> sstr

sample_stats %>%
use_series(sse) %>%
sum() %>%
round(3) -> ssee

total <- round(sstr + ssee, 3)
df_sstr <- nrow(sample_stats) - 1
df_sse <- nrow(data) - nrow(sample_stats)
df_sst <- nrow(data) - 1
mstr <- round(sstr / df_sstr, 3)
mse <- round(ssee / df_sse, 3)
f <- round(mstr / mse, 3)
sig <- round(1 - pf(f, df_sstr, df_sse), 3)
obs <- nrow(data)
regs <- paste(var_names[1], "~ as.factor(", var_names[2], ")")
model <- lm(as.formula(regs), data = data)
reg <- summary(model)

out <- list(
sstr = sstr, ssee = ssee, total = total, df_sstr = df_sstr,
df_sse = df_sse, df_sst = df_sst, mstr = mstr, mse = mse, f = f,
sig = sig, obs = obs, model = model, reg = reg
)

return(out)
}
Loading

0 comments on commit 7b8a7cc

Please sign in to comment.