Skip to content

Commit

Permalink
lilliefors correction in ksnormal test
Browse files Browse the repository at this point in the history
  • Loading branch information
abusjahn committed Aug 1, 2024
1 parent 7629f14 commit d9d6b6f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 48 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Description: The main functionalities of 'wrappedtools' are:
descriptive statistics and p-values; creating specialized plots for
correlation matrices. Functions were mainly written for my own daily work or
teaching, but may be of use to others as well.
Version: 0.9.5
Date: 2024-03-16
Version: 0.9.6
Date: 2024-07-30
Authors@R: c(
person(given = "Andreas",
family = "Busjahn",
Expand Down Expand Up @@ -46,11 +46,12 @@ Imports:
broom,
rlist,
DescTools,
flextable
flextable,
nortest
Depends:
R (>= 4.2)
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
LazyData: true
VignetteBuilder: knitr
Suggests: rmarkdown, testthat, ggrepel
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#wrappedtools 0.9.5 combines all updates from minor releases 9.4.x
#wrappedtools 0.9.6
- function ksnormal now uses Lilliefors test by default

#wrappedtools 0.9.5
- combines all updates from minor releases 9.4.x

#wrappedtools 0.9.4
- ksnormal can handle input of 0 data points
Expand Down
83 changes: 46 additions & 37 deletions R/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ pairwise_ordcat_test <- function(dep_var, indep_var, adjmethod = "fdr", plevel =
#' If less than 2 values are provided, NA is returned.
#'
#' @param x Vector of data to test.
#' @param lillie Logical, should the Lilliefors test be used? Defaults to TRUE
#'
#' @return p.value from \link{ks.test}.
#'
Expand All @@ -194,14 +195,22 @@ pairwise_ordcat_test <- function(dep_var, indep_var, adjmethod = "fdr", plevel =
#' sd = sd(mtcars$wt, na.rm = TRUE)
#' )
#' # wrapped version:
#' ksnormal(x = mtcars$wt)
#' ksnormal(x = mtcars$wt, lillie=FALSE)
#' @export
ksnormal <- function(x) {
ksnormal <- function(x, lillie=TRUE) {
if(length(na.omit(x))>1){
suppressWarnings(
assign("ksout",ks.test(x, "pnorm", mean(x, na.rm = TRUE), sd(x, na.rm = TRUE),
exact = FALSE
)$p.value))
if(lillie){
assign("ksout",
nortest::lillie.test(x)$p.value)
} else{
suppressWarnings(
assign("ksout",
ks.test(x, "pnorm", mean(x, na.rm = TRUE),
sd(x, na.rm = TRUE),
exact = FALSE
)$p.value))
}

}else{
ksout <- NA
}
Expand Down Expand Up @@ -619,22 +628,22 @@ compare2qualvars <- function(data, dep_vars, indep_var,
data |>
select(all_of(c(indep_var,var_i))) |>
mutate(testvar=forcats::fct_collapse(!!sym(var_i),
check=subgroups[sg_i],
other_level = 'other')) |>
check=subgroups[sg_i],
other_level = 'other')) |>
select(all_of(indep_var),'testvar') |> table()
if(ncol(testdata)>1) {
p_sg <- fisher.test(testdata,
simulate.p.value = TRUE,
B = 10^5)$p.value |>
formatP(mark = mark, pretext = pretext)
simulate.p.value = TRUE,
B = 10^5)$p.value |>
formatP(mark = mark, pretext = pretext)
} else{
p_sg <- ''
}
if(singleline){
freqBYgroup[[var_i]]$p <-
paste(na.omit(freqBYgroup[[var_i]]$p),p_sg) |>
str_squish()} else {
freqBYgroup[[var_i]]$p[sg_i] <- p_sg
freqBYgroup[[var_i]]$p[sg_i] <- p_sg
}
}
}
Expand All @@ -650,36 +659,36 @@ compare2qualvars <- function(data, dep_vars, indep_var,
for (var_i in seq_along(dep_vars)) {
if (!singleline) {
out_tmp <- add_row(out[0,],
Variable = c(
dep_vars[var_i],
glue::glue(
"{indentor}{levels[[var_i]][[1]]}"
)
),
desc_all = c(spacer, freq[[var_i]][[1]]),
g1 = c(spacer, freqBYgroup[[var_i]][[1]]),
g2 = c(spacer, freqBYgroup[[var_i]][[2]]),
p = c(
p[[var_i]][[1]],
rep(spacer, nrow(freqBYgroup[[var_i]]))
)
Variable = c(
dep_vars[var_i],
glue::glue(
"{indentor}{levels[[var_i]][[1]]}"
)
),
desc_all = c(spacer, freq[[var_i]][[1]]),
g1 = c(spacer, freqBYgroup[[var_i]][[1]]),
g2 = c(spacer, freqBYgroup[[var_i]][[2]]),
p = c(
p[[var_i]][[1]],
rep(spacer, nrow(freqBYgroup[[var_i]]))
)
)
if(p_subgroups){
out_tmp$pSubgroup <- c(spacer,freqBYgroup[[var_i]]$p)
}
out <- rbind(out,out_tmp)
} else {
out_tmp <- add_row(out[0,],
Variable = paste(
dep_vars[var_i],
# rep(spacer,
# nrow(freqBYgroup[[var_i]])-1)),
levels[[var_i]][[1]]
),
desc_all = freq[[var_i]][[1]],
g1 = freqBYgroup[[var_i]][[1]],
g2 = freqBYgroup[[var_i]][[2]],
p = p[[var_i]][[1]]
Variable = paste(
dep_vars[var_i],
# rep(spacer,
# nrow(freqBYgroup[[var_i]])-1)),
levels[[var_i]][[1]]
),
desc_all = freq[[var_i]][[1]],
g1 = freqBYgroup[[var_i]][[1]],
g2 = freqBYgroup[[var_i]][[2]],
p = p[[var_i]][[1]]
)
if(p_subgroups){
out_tmp$pSubgroup <- freqBYgroup[[var_i]]$p
Expand Down Expand Up @@ -1142,7 +1151,7 @@ compare_n_numvars <- function(.data = rawdata,
collapse = ";")) |>
pivot_longer(everything(),names_to = 'Variable',
values_to = 'p between groups')) |>
# gather(key = "Variable", value = "p between groups")) |>
# gather(key = "Variable", value = "p between groups")) |>
full_join(purrr::reduce(t$p_wcox_t_out, rbind) |>
matrix(nrow = length(dep_vars), byrow = FALSE) |>
as_tibble(.name_repair = "unique") |>
Expand All @@ -1155,7 +1164,7 @@ compare_n_numvars <- function(.data = rawdata,
)) |>
pivot_longer(everything(),names_to = 'Variable',
values_to = 'p vs.ref'))
# gather(key = "Variable", value = "p vs.ref"))
# gather(key = "Variable", value = "p vs.ref"))
results <- cbind(
results,
purrr::map2_df(
Expand Down
6 changes: 4 additions & 2 deletions man/ksnormal.Rd

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

Binary file modified tests/testthat/ksnormal_out.rda
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/testthat/test-ksnormal.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# out1 <- ksnormal(x = mtcars$wt)
# out1 <- ksnormal(x = mtcars$wt,lillie=FALSE)
# out2 <- suppressWarnings(ks.test(
# x = mtcars$wt, pnorm, mean = mean(mtcars$wt, na.rm = TRUE),
# sd = sd(mtcars$wt, na.rm = TRUE))$p.value
# )
# saveRDS(list(out1=out1, out2=out2),file = 'tests/testthat/ksnormal_out.rda')

test_that("ksnormal() with defaults", {
test_that("ksnormal() without Lilliefors", {
expected <- readRDS('ksnormal_out.rda')
expect_equal(ksnormal(x = mtcars$wt), expected[[1]])
expect_equal(ksnormal(x = mtcars$wt),expected[[2]])
expect_equal(ksnormal(x = mtcars$wt,lillie = FALSE), expected[[1]])
expect_equal(ksnormal(x = mtcars$wt, lillie = FALSE),expected[[2]])
})

0 comments on commit d9d6b6f

Please sign in to comment.