tests/testthat/test_test_wrapper_1.R

context("tests for wrappers of tests")
test_df <- data.frame(
  cat=rep(c("cat1", "cat2"), 20),
  dim = sort(rep(paste0("dim", seq(4)), 5)),
  dim_na=c(paste0("dim", seq(10)), paste0("dim", seq(10)+3)))

test_df$list_c <- as.list(seq(20))

test_df[["with space"]] <- seq(20)

test_that("test t.test.aggregated with two.sided alternative (default)", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  # Compare the outputs between stats::t.test and exploratory:::t.test.aggregated
  res0 <- stats::t.test(data=test_df, val~cat)
  res <- exploratory:::t.test.aggregated(test_df2$n[1],test_df2$n[2],test_df2$mean[1],test_df2$mean[2],test_df2$sd[1],test_df2$sd[2],0.95,0)
  expect_equal(res$statistic, res0$statistic)
  expect_equal(res$parameter, res0$parameter)
  expect_equal(res$p.value, res0$p.value)
  expect_equal(res$conf.int, res0$conf.int)
  names(res0$estimate) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$estimate, res0$estimate)
  expect_equal(res$stderr, res0$stderr)
  names(res0$null.value) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$null.value, res0$null.value)
})

test_that("test t.test.aggregated with 'less' alternative", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  # Compare the outputs between stats::t.test and exploratory:::t.test.aggregated
  res0 <- stats::t.test(data=test_df, val~cat, alternative="less")
  res <- exploratory:::t.test.aggregated(test_df2$n[1],test_df2$n[2],test_df2$mean[1],test_df2$mean[2],test_df2$sd[1],test_df2$sd[2],0.95,0, alternative="less")
  expect_equal(res$statistic, res0$statistic)
  expect_equal(res$parameter, res0$parameter)
  expect_equal(res$p.value, res0$p.value)
  expect_equal(res$conf.int, res0$conf.int)
  names(res0$estimate) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$estimate, res0$estimate)
  expect_equal(res$stderr, res0$stderr)
  expect_equal(res$alternative, res0$alternative)
  names(res0$null.value) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$null.value, res0$null.value)
})
test_that("test t.test.aggregated with 'greater' alternative", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  # Compare the outputs between stats::t.test and exploratory:::t.test.aggregated
  res0 <- stats::t.test(data=test_df, val~cat, alternative="greater")
  res <- exploratory:::t.test.aggregated(test_df2$n[1],test_df2$n[2],test_df2$mean[1],test_df2$mean[2],test_df2$sd[1],test_df2$sd[2],0.95,0, alternative="greater")
  expect_equal(res$statistic, res0$statistic)
  expect_equal(res$parameter, res0$parameter)
  expect_equal(res$p.value, res0$p.value)
  expect_equal(res$conf.int, res0$conf.int)
  names(res0$estimate) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$estimate, res0$estimate)
  expect_equal(res$stderr, res0$stderr)
  expect_equal(res$alternative, res0$alternative)
  names(res0$null.value) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$null.value, res0$null.value)
})

test_that("test t.test.aggregated with equal variance assumption with two.sided alternative", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  # Compare the outputs between stats::t.test and exploratory:::t.test.aggregated
  res0 <- stats::t.test(data=test_df, val~cat, var.equal=TRUE)
  res <- exploratory:::t.test.aggregated(test_df2$n[1],test_df2$n[2],test_df2$mean[1],test_df2$mean[2],test_df2$sd[1],test_df2$sd[2],0.95,0, var.equal=TRUE)
  expect_equal(res$statistic, res0$statistic)
  expect_equal(res$parameter, res0$parameter)
  expect_equal(res$p.value, res0$p.value)
  expect_equal(res$conf.int, res0$conf.int)
  names(res0$estimate) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$estimate, res0$estimate)
  expect_equal(res$stderr, res0$stderr)
  names(res0$null.value) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$null.value, res0$null.value)
})

test_that("test t.test.aggregated with equal variance assumption with greater alternative", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  # Compare the outputs between stats::t.test and exploratory:::t.test.aggregated
  res0 <- stats::t.test(data=test_df, val~cat, var.equal=TRUE, alternative='greater')
  res <- exploratory:::t.test.aggregated(test_df2$n[1],test_df2$n[2],test_df2$mean[1],test_df2$mean[2],test_df2$sd[1],test_df2$sd[2],0.95,0, var.equal=TRUE, alternative='greater')
  expect_equal(res$statistic, res0$statistic)
  expect_equal(res$parameter, res0$parameter)
  expect_equal(res$p.value, res0$p.value)
  expect_equal(res$conf.int, res0$conf.int)
  names(res0$estimate) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$estimate, res0$estimate)
  expect_equal(res$stderr, res0$stderr)
  names(res0$null.value) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$null.value, res0$null.value)
})

test_that("test t.test.aggregated with equal variance assumption with less alternative", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  # Compare the outputs between stats::t.test and exploratory:::t.test.aggregated
  res0 <- stats::t.test(data=test_df, val~cat, var.equal=TRUE, alternative='less')
  res <- exploratory:::t.test.aggregated(test_df2$n[1],test_df2$n[2],test_df2$mean[1],test_df2$mean[2],test_df2$sd[1],test_df2$sd[2],0.95,0, var.equal=TRUE, alternative='less')
  expect_equal(res$statistic, res0$statistic)
  expect_equal(res$parameter, res0$parameter)
  expect_equal(res$p.value, res0$p.value)
  expect_equal(res$conf.int, res0$conf.int)
  names(res0$estimate) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$estimate, res0$estimate)
  expect_equal(res$stderr, res0$stderr)
  names(res0$null.value) <- NULL # Ignore names difference, which we did not implement.
  expect_equal(res$null.value, res0$null.value)
})

test_that("test exp_ttest_aggregated", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )
  test_df2 <- test_df %>% group_by(cat) %>% summarize(n=n(), sd=sd(val), mean=mean(val))
  model_df <- test_df2 %>% exp_ttest_aggregated(cat, n, mean, sd)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_true("Rows" %in% colnames(ret))
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  expect_true("p.value" %in% colnames(ret))
})

test_that("test two sample t-test with column name", {
  test_df <- data.frame(
    cat=rep(c("cat1", "cat2"), 20),
    val = rep(seq(10), 2)
  )

  result <- test_df %>%
    do_t.test(val, cat)

  expect_equal(result$mean_cat1, 5)
  expect_equal(result$mean_cat2, 6)

  # swap cat1 and cat2
  test_df <- data.frame(
    cat=rep(c("cat2", "cat1"), 20),
    val = rep(seq(10), 2)
  )

  result <- test_df %>%
    do_t.test(val, cat)

  expect_equal(result$mean_cat1, 6)
  expect_equal(result$mean_cat2, 5)
})

test_that("test two sample t-test with factor", {
  test_df <- data.frame(
    cat=factor(rep(c("cat1", "cat2"), 20), levels = c("cat1", "cat2")),
    val = rep(seq(10), 2)
  )

  result <- test_df %>%
    do_t.test(val, cat)

  expect_equal(result$mean_cat1, 5)
  expect_equal(result$mean_cat2, 6)

  # swap cat1 and cat2
  test_df <- data.frame(
    cat=factor(rep(c("cat2", "cat1"), 20), levels = c("cat2", "cat1")),
    val = rep(seq(10), 2)
  )

  result <- test_df %>%
    do_t.test(val, cat)

  expect_equal(result$mean_cat1, 6)
  expect_equal(result$mean_cat2, 5)
})

test_that("test two sample t-test with logical", {
  test_df <- data.frame(
    cat=rep(c(TRUE, FALSE), 20),
    val = rep(seq(10), 2)
  )

  result <- test_df %>%
    do_t.test(val, cat)

  expect_equal(result$mean_TRUE, 5)
  expect_equal(result$mean_FALSE, 6)

  # swap TRUE and FALSE
  test_df <- data.frame(
    cat=rep(c(FALSE, TRUE), 20),
    val = rep(seq(10), 2)
  )

  result <- test_df %>%
    do_t.test(val, cat)

  expect_equal(result$mean_FALSE, 5)
  expect_equal(result$mean_TRUE, 6)
})

test_that("test two sample t-test more than 2 levels", {
  expect_error({
    result <- test_df %>%
      dplyr::group_by(dim) %>%
      do_t.test(`with space`, dim_na)
  })
})

test_that("test two sample t-test less than 2 levels", {
  expect_error({
    result <- test_df %>%
      dplyr::group_by(dim) %>%
      do_t.test(`with space`, dim)
  })
})

test_that("test one sample t-test", {
  result <- test_df %>%
    dplyr::group_by(dim) %>%
    do_t.test("with space", mu=3)
  expect_equal(result[result[["dim"]]=="dim1", "p.value"][[1]], 1)
})

test_that("test t-test with 3 groups", {
  data <- data.frame(val = seq(12), group = rep(c(1,2,3), each = 4))
  expect_error({
    result <- data %>%
      do_t.test(val, group)
  }, "Group Column has to have 2 unique values")
})

test_that("test f-test", {
  result <- test_df %>%
    dplyr::group_by(dim) %>%
    do_var.test(`with space`, cat)
  expect_equal(ncol(result), 10)
})

test_that("test f-test with 3 groups", {
  data <- data.frame(val = seq(12), group = rep(c(1,2,3), each = 4))
  expect_error({
    result <- data %>%
      do_var.test(val, group)
  }, "Group Column has to have 2 unique values")
})

test_that("test chisq.test with one column", {
  test_df <- data.frame(
    group = rep(letters[1:2], each = 500),
    cat1 = letters[round(runif(1000)*5)+1],
    cat2 = letters[round(runif(1000)*3)+1]
  ) %>%
    dplyr::group_by(group, cat1, cat2) %>%
    dplyr::summarize(count = n()) %>%
    dplyr::group_by(group)

  ret <- test_df %>%
    do_chisq.test(count)

  expect_equal(nrow(ret), 2)

})

test_that("test chisq.test with select argument", {
  test_df <- data.frame(
    group = rep(letters[1:2], each = 500),
    cat1 = letters[round(runif(1000)*5)+1],
    cat2 = letters[round(runif(1000)*3)+1]
  ) %>%
    dplyr::group_by(group, cat1, cat2) %>%
    dplyr::summarize(count = n()) %>%
    tidyr::spread(cat2, count) %>%
    dplyr::group_by(group)

  ret <- test_df %>%
    do_chisq.test(-cat1)

  expect_equal(nrow(ret), 2)

})

test_that("test chisq.test with p column", {
  test_df <- structure(
    list(
      clarity = c("IF", "VS1", "VS2", "VVS1", "VVS2"),
      GIA = c(6, 61, 36, 15, 33),
      HRD = c(4, 13, 15, 23, 24),
      IGI = c(34, 7, 2, 14, 21)),
    .Names = c("clarity", "GIA", "HRD", "IGI"),
    class = "data.frame",
    row.names = c(NA,-5L)) %>%
    dplyr::mutate(p = seq(5))

  ret <- test_df %>%
    do_chisq.test(GIA, p = p)
  expect_equal(nrow(ret), 1)

  p_from_outside <- seq(5)

  ret2 <- test_df %>%
    do_chisq.test(IGI, p = p_from_outside)
  expect_equal(nrow(ret), 1)

  ret3 <- test_df %>%
    do_chisq.test(IGI, p = c(1, 2, 3, 4, 5))
  expect_equal(nrow(ret), 1)

})

test_that("test exp_chisq", {
  mtcars2 <- mtcars
  mtcars2$gear[[1]] <- NA # test handling of NAs
  mtcars2$carb[[2]] <- NA
  mtcars2$cyl[[3]] <- NA
  ret <- exp_chisq(mtcars2 %>% mutate(gear=factor(gear)), gear, carb) # factor order should be kept in the model
  ret <- exp_chisq(mtcars2, gear, carb, value=cyl, fun.aggregate=sum)

  # Test model_info function.
  # Rename model column so that we test the case where the column name is not "model". There was an issue this case.
  observed <- ret %>% rename(model1=model) %>% model_info(model1, output="variables", type="observed")
  summary <- ret %>% rename(model1=model) %>% model_info(model1, output="summary")
  data <- ret %>% rename(model1=model) %>% model_info(model1, output="data")

  observed <- ret %>% tidy_rowwise(model, type="observed")
  summary <- ret %>% glance_rowwise(model)
  residuals <- ret %>% tidy_rowwise(model, type="residuals")
  expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W",
                    "Power", "Type 2 Error","Rows") %in% colnames(summary)
  ))
  expect_true(summary$`Cramer's V` >= 0 && summary$`Cramer's V` <= 1)
  prob_dist <- ret %>% tidy_rowwise(model, type="prob_dist")
  expect_true("p.value" %in% colnames(prob_dist))
})

test_that("test exp_chisq with power", {
  model_df <- exp_chisq(mtcars %>% mutate(gear=factor(gear)), gear, carb, power = 0.8) # factor order should be kept in the model
  ret <- model_df %>% glance_rowwise(model)
  model_df <- exp_chisq(mtcars, gear, carb, value=cyl, power = 0.8)
  ret <- model_df %>% glance_rowwise(model)
  expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W",
                     "Target Power","Target Type 2 Error","Current Sample Size","Required Sample Size") %in% colnames(ret)
  ))
  expect_true(ret$`Cramer's V` >= 0 && ret$`Cramer's V` <= 1)
})

test_that("test exp_chisq with grouping functions", {
  model_df <- exp_chisq(mtcars, disp, drat, func1="asintby10", func2="asint", value=mpg)
  ret <- model_df %>% glance_rowwise(model)
  expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W","Power",
                 "Type 2 Error","Rows") %in% colnames(ret)
  ))
  expect_true(ret$`Cramer's V` >= 0 && ret$`Cramer's V` <= 1)
})

test_that("test exp_chisq with logical", {
  model_df <- exp_chisq(mtcars %>% mutate(gear=gear>3, carb=carb>3), gear, carb) # logical type should be kept in the model
  ret <- model_df %>% tidy_rowwise(model, type="residuals")
  expect_equal(class(ret$gear), "logical")
  expect_equal(class(ret$carb), "logical")
})

test_that("test exp_chisq with numeric", {
  model_df <- exp_chisq(mtcars , gear, carb) # numeric type should be kept in the model
  ret <- model_df %>% tidy_rowwise(model, type="residuals")
  expect_equal(class(ret$gear), "numeric")
  expect_equal(class(ret$carb), "numeric")
})
test_that("test exp_chisq with integer", {
  model_df <- exp_chisq(mtcars %>% mutate(gear=as.integer(gear), carb=as.integer(carb)), gear, carb) # integer type should be kept in the model
  ret <- model_df %>% tidy_rowwise(model, type="residuals")
  expect_equal(class(ret$gear), "integer")
  expect_equal(class(ret$carb), "integer")
})
test_that("test exp_chisq with group_by", {
  ret <- mtcars %>% group_by(vs) %>% exp_chisq(gear, carb, value=cyl)
  observed <- ret %>% tidy_rowwise(model, type="observed")
  expect_true("vs" %in% colnames(observed))
  summary <- ret %>% glance_rowwise(model)
  expect_true("vs" %in% colnames(summary))
  residuals <- ret %>% tidy_rowwise(model, type="residuals")
  expect_true("vs" %in% colnames(residuals))
})

test_that("test exp_chisq with group_by with single class category in one of the groups", {
  ret <- mtcars %>% filter(vs!=1 | gear==4) %>% group_by(vs) %>% exp_chisq(gear, carb, value=cyl)
  observed <- ret %>% tidy_rowwise(model, type="observed")
  summary <- ret %>% glance_rowwise(model)
  expect_equal(nrow(summary), 2) # summary for 2 groups, one of which is a row with Note, should be shown.
  residuals <- ret %>% tidy_rowwise(model, type="residuals")
})

test_that("test exp_chisq_ab_aggregated", {
  df <- tibble::tibble(cat=c('A','B'), n=c(100,200), cr=c(0.22, 0.2))
  ret <- df %>% exp_chisq_ab_aggregated(cat, cr, n)

  observed <- ret %>% tidy_rowwise(model, type="observed")
  summary <- ret %>% glance_rowwise(model)
  residuals <- ret %>% tidy_rowwise(model, type="residuals")
  expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W",
                    "Power", "Type 2 Error","Rows") %in% colnames(summary)
  ))
  expect_true(summary$`Cramer's V` >= 0 && summary$`Cramer's V` <= 1)
  prob_dist <- ret %>% tidy_rowwise(model, type="prob_dist")
})

test_that("test exp_chisq_ab_aggregated with multiple rows per group", {
  # Here we test the case where a group (A or B) has more than 1 row.
  # It's not the most common use we expect, but this should also work.
  df <- tibble::tibble(cat=rep(c('A','B'),2), n=rep(c(100,200),2), cr=rep(c(0.22, 0.2),2))
  ret <- df %>% exp_chisq_ab_aggregated(cat, cr, n)
  observed <- ret %>% tidy_rowwise(model, type="observed")
  summary <- ret %>% glance_rowwise(model)
  expect_equal(summary$`Rows`,600) # Number of rows should be added up.
  expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W",
                    "Power", "Type 2 Error","Rows") %in% colnames(summary)
  ))
  expect_true(summary$`Cramer's V` >= 0 && summary$`Cramer's V` <= 1)
  residuals <- ret %>% tidy_rowwise(model, type="residuals")
  prob_dist <- ret %>% tidy_rowwise(model, type="prob_dist")
})

test_that("test exp_chisq_ab_aggregated with more than 2 groups", {
  # Here we test the case where there are more than 2 groups.
  # It's not the most common use we expect, but this should also work as 2xN chi-square test.
  df <- tibble::tibble(cat=c('A','B','C'), n=c(100,200,300), cr=c(0.22, 0.2, 0.2))
  model_df <- df %>% exp_chisq_ab_aggregated(cat, cr, n)
  summary <- model_df %>% glance_rowwise(model)
  expect_equal(summary$`Rows`,600) # Number of rows should be added up.
  expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W",
                    "Power", "Type 2 Error","Rows") %in% colnames(summary)
  ))
  expect_true(summary$`Cramer's V` >= 0 && summary$`Cramer's V` <= 1)
  residuals <- model_df %>% tidy_rowwise(model, type="residuals")
  prob_dist <- model_df %>% tidy_rowwise(model, type="prob_dist")
})

test_that("test exp_chisq_ab_aggregated with only group A", {
  df <- tibble::tibble(cat=c('A'), n=c(100), cr=c(0.22))
  expect_error({
    model_df <- df %>% exp_chisq_ab_aggregated(cat, cr, n)
  }, "The explanatory variable needs to have 2 or more unique values.")
})

test_that("test exp_ttest", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, test_sig_level=0.05)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_true("Rows" %in% colnames(ret))
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  expect_true("p.value" %in% colnames(ret))

})

test_that("test exp_ttest with factor explanatory variable", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  # Put unused factor levels too for test.
  mtcars2 <- mtcars2 %>% dplyr::mutate(am=factor(am, levels=c(-1,0,1,2)))
  model_df <- exp_ttest(mtcars2, mpg, am)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(ret$`Base Level`, "0") # First *used* factor level should be the base.
  expect_gt(ret$Difference, 0) # Checking the direction of Difference is correct.
  expect_true("Rows" %in% colnames(ret))
  model_df %>% tidy_rowwise(model, type="data_summary")
})

test_that("test exp_ttest with numeric explanatory variable", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(ret$`Base Level`, "0") # The smaller number should be the base.
  expect_gt(ret$Difference, 0) # Checking the direction of Difference is correct.
  expect_true("Rows" %in% colnames(ret))
  model_df %>% tidy_rowwise(model, type="data_summary")
})

test_that("test exp_ttest with character explanatory variable", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  mtcars2 <- mtcars2 %>% dplyr::mutate(am=as.character(am))
  model_df <- exp_ttest(mtcars2, mpg, am)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(ret$`Base Level`, "0") # The majority should be the base
  expect_gt(ret$Difference, 0) # Checking the direction of Difference is correct.
  expect_true("Rows" %in% colnames(ret))
  model_df %>% tidy_rowwise(model, type="data_summary")
})

test_that("test exp_ttest with logical explanatory variable", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  mtcars2 <- mtcars2 %>% dplyr::mutate(am=as.logical(am))
  model_df <- exp_ttest(mtcars2, mpg, am)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(ret$`Base Level`, "FALSE") # FALSE should be the base
  expect_gt(ret$Difference, 0) # Checking the direction of Difference is correct.
  expect_true("Rows" %in% colnames(ret))
  model_df %>% tidy_rowwise(model, type="data_summary")
})

test_that("test exp_ttest with var.equal = TRUE", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, var.equal = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
})

test_that("test exp_ttest with alternative = greater", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, alternative = "greater")
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  colnames(ret)
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
})

test_that("test exp_ttest with paired = TRUE", {
  # Make sample size equal between groups for paired t-test.
  mtcars2 <- mtcars %>% group_by(am) %>% slice_sample(n=6) %>% ungroup()
  model_df <- exp_ttest(mtcars2, mpg, am, paired = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
  ret
})

test_that("test exp_ttest with power", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, beta = 0.2)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(colnames(ret),
               c("t Value","P Value","DF","Difference",
                 "Conf Low","Conf High","Base Level","Cohen's D","Target Power",
                 "Target Type 2 Error","Current Sample Size (Each Group)","Required Sample Size (Each Group)","Rows",
                 "Rows (0)","Rows (1)"))
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
})

test_that("test exp_ttest with power with paired = TRUE", {
  # Make sample size equal between groups for paired t-test.
  mtcars2 <- mtcars %>% group_by(am) %>% slice_sample(n=6) %>% ungroup()
  model_df <- exp_ttest(mtcars2, mpg, am, paired = TRUE, power = 0.8)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
  ret
})


test_that("test exp_ttest with diff_to_detect", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, diff_to_detect = 0.5, power = 0.8)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
  ret
})

test_that("test exp_ttest with diff_to_detect and common_sd", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, diff_to_detect = 0.5, common_sd = 1.5, power = 0.8)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
})


test_that("test exp_ttest with asint grouping", {
  mtcars2 <- mtcars
  mtcars2$am[[1]] <- NA # test NA filtering
  model_df <- exp_ttest(mtcars2, mpg, am, func2 = "asint")
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
})

test_that("test exp_ttest with group_by", {
  model_df <- mtcars %>% group_by(vs) %>% exp_ttest(mpg, am)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("vs","am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
  ret
})

test_that("test exp_ttest with outlier filter", {
  model_df <- mtcars %>% group_by(vs) %>% exp_ttest(mpg, am, outlier_filter_type="percentile", outlier_filter_threshold=0.9)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("vs","am","Rows","Mean","Conf Low","Conf High","Std Error of Mean","Std Deviation",
                 "Minimum","Maximum"))
})

test_that("test exp_ttest with group-level error (lack of unique values)", {
  df <- tibble::tibble(group=c(1,1,2,2),category=c("a","a","b","b"),value=c(1,2,1,2))
  model_df <- df %>% dplyr::group_by(`group`) %>% exp_ttest(`value`, `category`)
  ret <- model_df %>% tidy_rowwise(model, type='model')
  expect_equal(colnames(ret),
               c("group","Note"))
  ret <- model_df %>% tidy_rowwise(model, type='prob_dist')
  expect_equal(nrow(ret), 0)
})

test_that("test exp_ttest with group-level error (not eough data)", {
  df <- tibble::tibble(group=c(1,1,2,2),category=c("a","b","a","b"),value=c(1,2,1,2))
  model_df <- df %>% dplyr::group_by(`group`) %>% exp_ttest(`value`, `category`)
  ret <- model_df %>% tidy_rowwise(model, type='model')
  expect_equal(colnames(ret),
               c("group", "Rows", "Rows (a)", "Rows (b)", "Note"))
  ret <- model_df %>% tidy_rowwise(model, type='prob_dist')
  expect_equal(nrow(ret), 0)
})

test_that("test 2-way ANOVA with exp_anova", {
  mtcars2 <- mtcars %>% mutate(`a m`=am, `ge ar`=gear, `w t`=wt, `q sec`=qsec)
  model_df <- mtcars2 %>% exp_anova(mpg, c("a m","ge ar"), func2=c("aschar","aschar"))
  # This case fails with multi-colinearity-related error.
  # model_df <- mtcars2 %>% exp_anova(mpg, c("carb","ge ar"), func2=c("aschar","aschar"), with_interaction = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="model")
  # Make sure the estimate is between conf.low and conf.high.
  expect_equal(all(ret$`Difference` >= ret$`Conf Low`), TRUE)
  expect_equal(all(ret$`Difference` <= ret$`Conf High`), TRUE)

  ret <- model_df %>% tidy_rowwise(model, type="emmeans", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="data")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  ret <- model_df %>% tidy_rowwise(model, type="pairs_per_variable", pairs_adjust="tukey")
})

test_that("test 2-way ANOVA with with different data types on var1 and var2.", {
  mtcars2 <- mtcars %>% mutate(`a m`=am == 1, `ge ar`=as.character(gear))
  model_df <- mtcars2 %>% exp_anova(mpg, c("a m","ge ar"), func2=c("aschar","aschar"))
  
  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="model")
  # Make sure the estimate is between conf.low and conf.high.
  expect_equal(all(ret$`Difference` >= ret$`Conf Low`), TRUE)
  expect_equal(all(ret$`Difference` <= ret$`Conf High`), TRUE)

  ret <- model_df %>% tidy_rowwise(model, type="emmeans", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="data")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  # Make sure this doesn't fail.
  ret <- model_df %>% tidy_rowwise(model, type="pairs_per_variable", pairs_adjust="tukey")
  # Make sure the output for "Group  1" and "Group  2" is in character.
  expect_equal(ret$`Group 1`, c("FALSE", "3", "3", "4"))
  expect_equal(ret$`Group 2`, c("TRUE", "4", "5", "5"))
})

test_that("test 2-way ANOVA with exp_anova with repeat-by", {
  mtcars2 <- mtcars %>% mutate(`a m`=am, `ge ar`=gear, `w t`=wt, `q sec`=qsec) %>% group_by(vs)
  model_df <- mtcars2 %>% exp_anova(mpg, c("a m","ge ar"), func2=c("aschar","aschar"))
  # This case fails with multi-colinearity-related error.
  # model_df <- mtcars2 %>% exp_anova(mpg, c("carb","ge ar"), func2=c("aschar","aschar"), with_interaction = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(colnames(ret),
    c("vs","Variable","Sum of Squares","SS Ratio","DF","Mean Square","F Value","P Value","Eta Squared","Partial Eta Squared","Cohen's F","Omega Squared","Note"))
  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  expect_equal(colnames(ret),
    c("vs","Group 1","Group 2","Difference",
      "Conf Low", "Conf High","Standard Error","DF","t Value","P Value","Method"))
  ret <- model_df %>% tidy_rowwise(model, type="emmeans", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  expect_true("p.value" %in% colnames(ret))

  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="data")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
})

test_that("test ANCOVA with exp_anova", {
  mtcars2 <- mtcars %>% mutate(`a m`=factor(am), `w t`=wt, `q sec`=qsec)
  model_df <- mtcars2 %>% exp_anova(mpg, `a m`, covariates=c("w t", "q sec"),
                                    covariate_funs=list("w t"="log", "q sec"="none"),
                                    with_interaction = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="emmeans", pairs_adjust="tukey")

  expect_equal(colnames(ret),
    c("a m", "w t", "Rows", "Mean", "Std Deviation", "Std Error", 
    "Conf Low", "Conf High", "Mean (Adj)", "Std Error (Adj)", 
    "Conf Low (Adj)", "Conf High (Adj)", "DF", "Minimum", "Maximum"))

  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  expect_true("p.value" %in% colnames(ret))

  ret <- model_df %>% tidy_rowwise(model, type="anova")
  ret <- model_df %>% tidy_rowwise(model, type="data")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("a m","Rows","Mean","Std Error", "Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))

  # Test broom output to detect changes at upgrade.
  x <- model_df$model[[1]]
  ret <- broom::tidy(car::Anova(x, type="III"))
  expect_equal(colnames(ret),
               c("term", "sumsq", "df", "statistic", "p.value"))
  ret <- broom::tidy(car::leveneTest(x$residuals, x$data[[x$var2]], center=median))
  expect_equal(colnames(ret),
               c("statistic", "p.value", "df", "df.residual"))
  ret <- broom::tidy(shapiro.test(x$residuals))
  expect_equal(colnames(ret),
               c("statistic", "p.value", "method"))
})

test_that("test ANCOVA with exp_anova with NA and others in the variable.", {
  df<- readRDS(url("https://www.dropbox.com/scl/fi/ranvxxcihbdeosgvs7z96/28337.rds?rlkey=73xo6c8cig6k1o5xsu9v9gndb&dl=1"))
  model_df <- df %>% exp_anova(`出発 遅れ表`, `航空 会社表`, covariates = c("到着 遅れ表"), covariate_funs = c("到着 遅れ表" = "none"), with_interaction = TRUE)
  # Make sure type="levene" doesn't fail.
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="median")
  expect_equal(colnames(ret), c( "F Value", "P Value", "DF", "Residual DF", "Method" ,"Result"))
})

test_that("test ANCOVA with exp_anova with logical group variable", {
  mtcars2 <- mtcars %>% mutate(`a m`=as.logical(am), `w t`=wt, `q sec`=qsec)
  model_df <- mtcars2 %>% exp_anova(mpg, `a m`, covariates=c("w t", "q sec"),
                                    covariate_funs=list("w t"="log", "q sec"="none"),
                                    with_interaction = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="emmeans", pairs_adjust="tukey", sort_factor_levels=TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  ret <- model_df %>% tidy_rowwise(model, type="anova")
  ret <- model_df %>% tidy_rowwise(model, type="data")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("a m","Rows","Mean","Std Error","Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))

  # Test broom output to detect changes at upgrade.
  x <- model_df$model[[1]]
  ret <- broom::tidy(car::Anova(x, type="III"))
  expect_equal(colnames(ret),
               c("term", "sumsq", "df", "statistic", "p.value"))
  ret <- broom::tidy(car::leveneTest(x$residuals, x$data[[x$var2]], center=median))
  expect_equal(colnames(ret),
               c("statistic", "p.value", "df", "df.residual"))
  ret <- broom::tidy(shapiro.test(x$residuals))
  expect_equal(colnames(ret),
               c("statistic", "p.value", "method"))
})

test_that("test ANCOVA with repeat-by", {
  mtcars2 <- mtcars %>% mutate(`a m`=factor(am), `w t`=wt, `q sec`=qsec) %>% group_by(vs)
  model_df <- mtcars2 %>% exp_anova(mpg, `a m`, covariates=c("w t", "q sec"),
                                    covariate_funs=list("w t"="log", "q sec"="none"),
                                    with_interaction = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="emmeans", pairs_adjust="tukey", sort_factor_levels=TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  ret <- model_df %>% tidy_rowwise(model, type="anova")
  ret <- model_df %>% tidy_rowwise(model, type="data", sort_factor_levels=TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
})

test_that("test ANCOVA with exp_anova with some NAs in the data", {
  mtcars2 <- mtcars %>% mutate(`a m`=factor(am), `w t`=wt, `q sec`=qsec)
  mtcars2$`a m`[[1]] <- NA
  mtcars2$`w t`[[2]] <- NA
  mtcars2$`q sec`[[3]] <- NA
  model_df <- mtcars2 %>% exp_anova(mpg, `a m`, covariates=c("w t", "q sec"),
                                    covariate_funs=list("w t"="log", "q sec"="none"),
                                    with_interaction = TRUE)
  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  # TODO: make them work.
  # ret <- model_df %>% tidy_rowwise(model, type="levene")
  # ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="emmeans")
  ret <- model_df %>% tidy_rowwise(model, type="pairs")
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  ret <- model_df %>% tidy_rowwise(model, type="anova")
  ret <- model_df %>% tidy_rowwise(model, type="data")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("a m","Rows","Mean","Std Error","Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))
})

test_that("test exp_anova", {
  mtcars2 <- mtcars %>% mutate(`a m`=factor(am), `w t`=wt, `q sec`=qsec)
  model_df <- exp_anova(mtcars2, mpg, `a m`)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  expect_equal(nrow(ret), 3) # Between Groups, Within Group, and Total.
  # Make sure the estimate is between conf.low and conf.high.
  expect_equal(all(ret$`Difference` >= ret$`Conf Low`), TRUE)
  expect_equal(all(ret$`Difference` <= ret$`Conf High`), TRUE)

  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
  expect_true("p.value" %in% colnames(ret))

  ret <- model_df %>% tidy_rowwise(model, type="shapiro")
  ret <- model_df %>% tidy_rowwise(model, type="levene")
  ret <- model_df %>% tidy_rowwise(model, type="levene", levene_test_center="mean")
  ret <- model_df %>% tidy_rowwise(model, type="pairs", pairs_adjust="tukey")
  res <- model_df %>% tidy_rowwise(model, type="data", sort_factor_levels=TRUE)
  model_df <- exp_anova(mtcars, mpg, gear)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("gear","Rows","Mean","Std Error","Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))
  ret <- model_df %>% tidy_rowwise(model, type="prob_dist")
})

test_that("test exp_anova with logical group column", {
  mtcars2 <- mtcars %>% mutate(`a m`=as.logical(am), `w t`=wt, `q sec`=qsec)
  model_df <- exp_anova(mtcars2, mpg, `a m`)
  res <- model_df %>% tidy_rowwise(model, type="data", sort_factor_levels=TRUE)
})

test_that("test exp_anova with factor group column", {
  mtcars2 <- mtcars %>% mutate(`a m`=factor(am), `w t`=wt, `q sec`=qsec)
  model_df <- exp_anova(mtcars2, mpg, `a m`)
  res <- model_df %>% tidy_rowwise(model, type="data", sort_factor_levels=TRUE)
})

test_that("test exp_anova with group-level error (lack of unique values)", {
  df <- tibble::tibble(group=c(1,1,2,2),category=c("a","a","b","b"),value=c(1,2,1,2))
  model_df <- df %>% dplyr::group_by(`group`) %>% exp_anova(`value`, `category`)
  ret <- model_df %>% tidy_rowwise(model, type='model')
  expect_equal(colnames(ret),
               c("group","Note"))
  ret <- model_df %>% tidy_rowwise(model, type='prob_dist')
  expect_equal(nrow(ret), 0)
})

test_that("test exp_anova with group-level error (not enought data)", {
  df <- tibble::tibble(group=c(1,1,2,2),category=c("a","b","a","b"),value=c(1,2,1,2))
  model_df <- df %>% dplyr::group_by(`group`) %>% exp_anova(`value`, `category`)
  ret <- model_df %>% tidy_rowwise(model, type='model')
  expect_equal(colnames(ret),
               c("group","Rows", "Note"))
  ret <- model_df %>% tidy_rowwise(model, type='prob_dist')
  expect_equal(nrow(ret), 0)
})

test_that("test exp_anova with outlier filter", {
  model_df <- exp_anova(mtcars, mpg, am, outlier_filter_type="percentile", outlier_filter_threshold=0.9)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  model_df <- exp_anova(mtcars, mpg, gear, outlier_filter_type="percentile", outlier_filter_threshold=0.9)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("gear","Rows","Mean","Std Error", "Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))
})

test_that("test exp_anova with required power", {
  model_df <- exp_anova(mtcars, mpg, am, power=0.8)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  model_df <- exp_anova(mtcars, mpg, gear, power=0.8)
  ret <- model_df %>% tidy_rowwise(model, type="model")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("gear","Rows","Mean","Std Error", "Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))
})

test_that("test exp_anova with grouping functions", {
  model_df <- exp_anova(mtcars, mpg, disp, func2="asintby10")
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("disp","Rows","Mean","Std Error", "Conf Low","Conf High","Std Deviation",   
                 "Minimum","Maximum"))
})


test_that("test exp_anova with group_by", {
  model_df <- mtcars %>% group_by(vs) %>% exp_anova(mpg, am)
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  model_df <- mtcars %>% group_by(vs) %>% exp_anova(mpg, gear)
  ret <- model_df %>% tidy_rowwise(model, type="data_summary")
  expect_equal(colnames(ret),
               c("vs","gear","Rows","Mean","Std Error","Conf Low","Conf High",
                 "Std Deviation","Minimum","Maximum"))
})

test_that("test exp_normality", {
  df <- mtcars %>% mutate(dummy=c(NA, rep(1,n()-1))) # test for column with always same value, except for NA.
  ret <- df %>% exp_normality(mpg, gear, dummy, n_sample=20, n_sample_qq=30)
  qq <- ret %>% tidy_rowwise(model, type="qq")
  model_summary <- ret %>% tidy_rowwise(model, type="model_summary", signif_level=0.1)
  expect_equal(colnames(model_summary),
               c("Column","W Value","P Value","Sample Size","Result"))
})

test_that("test exp_normality with group", {
  df <- mtcars %>% mutate(dummy=c(NA, rep(1,n()-1))) %>% # test for column with always same value, except for NA.
    group_by(am)
  ret <- df %>% exp_normality(mpg, gear, dummy, n_sample=20, n_sample_qq=30)
  qq <- ret %>% tidy_rowwise(model, type="qq")
  expect_true("am" %in% colnames(qq))
  model_summary <- ret %>% tidy_rowwise(model, type="model_summary", signif_level=0.1)
  expect_true("am" %in% colnames(model_summary))
})

test_that("test exp_normality with column with almost always same value", {
  # test for column with almost always same value, except for NA, to test column prefiltering logic to avoid error.
  df <- mtcars %>% mutate(dummy=c(NA, 0, rep(1,n()-2)))
  ret <- df %>% exp_normality(mpg, gear, dummy, n_sample=6, n_sample_qq=30)
  qq <- ret %>% tidy_rowwise(model, type="qq")
  model_summary <- ret %>% tidy_rowwise(model, type="model_summary", signif_level=0.1)
  expect_equal(colnames(model_summary),
               c("Column","W Value","P Value","Sample Size","Result"))
})
exploratory-io/exploratory_func documentation built on April 23, 2024, 9:15 p.m.