Skip to content

Commit d687500

Browse files
committed
Implement chi-square gof
1 parent 60fa705 commit d687500

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

R/calculate.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ calculate <- function(x, stat, order = NULL, ...) {
7878
}
7979
}
8080

81-
if (stat %in% c("diff in means", "diff in medians", "diff in props") |
82-
attr(x, "theory_type") %in% c("Two sample props z", "Two sample t")) {
81+
if ( stat %in% c("diff in means", "diff in medians", "diff in props") ||
82+
attr(x, "theory_type") %in% c("Two sample props z", "Two sample t") ) {
8383
if (length(unique(x[[as.character(attr(x, "explanatory"))]])) != 2){
8484
stop(paste("Statistic is based on a difference; the explanatory variable",
8585
"should have two levels."))

R/set_params.R

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ set_params <- function(x){
44

55
attr(x, "theory_type") <- NULL
66

7+
if(!is.null(attr(x, "response"))){
8+
num_response_levels <- length(levels(x[[as.character(attr(x, "response"))]]))
9+
}
10+
711
# One variable
812
if (!is.null(attr(x, "response")) & is.null(attr(x, "explanatory")) &
913
!is.null(attr(x, "response_type")) & is.null(attr(x, "explanatory_type"))){
@@ -17,9 +21,12 @@ set_params <- function(x){
1721
}
1822

1923
# One prop
20-
if(attr(x, "response_type") == "factor"){
21-
attr(x, "theory_type") <- "One sample prop z"
24+
if(attr(x, "response_type") == "factor" & (num_response_levels == 2)){
2225
# No parameters since standard normal
26+
attr(x, "theory_type") <- "One sample prop z"
27+
} else {
28+
attr(x, "theory_type") <- "Chi-square Goodness of Fit"
29+
attr(x, "distr_param") <- num_response_levels - 1
2330
}
2431

2532
}
@@ -81,5 +88,9 @@ set_params <- function(x){
8188
# TO DO: Determine parameters
8289
}
8390
}
91+
92+
# if(is.null(attr(x, "theory_type")))
93+
# warning("Theoretical type not yet implemented")
94+
8495
x
8596
}

R/visualize.R

+10-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#' calculate(stat = "t", order = c("1", "0")) %>%
5454
#' visualize(method = "both")
5555

56-
visualize <- function(data, bins = 30, method = "randomization",
56+
visualize <- function(data, bins = 15, method = "randomization",
5757
dens_color = "black",
5858
obs_stat = NULL,
5959
obs_stat_color = "#00BFC4",
@@ -125,7 +125,8 @@ visualize <- function(data, bins = 30, method = "randomization",
125125
infer_plot <- theory_z_plot(statistic_text = "z")
126126
}
127127

128-
else if(attr(data, "theory_type") == "Chi-square test of indep"){
128+
else if(attr(data, "theory_type") %in%
129+
c("Chi-square test of indep", "Chi-square Goodness of Fit")){
129130
infer_plot <- theory_chisq_plot(deg_freedom = attr(data, "distr_param"),
130131
statistic_text = "Chi-Square",
131132
dens_color = dens_color)
@@ -170,7 +171,9 @@ visualize <- function(data, bins = 30, method = "randomization",
170171
obs_stat = obs_stat)
171172
}
172173

173-
else if(attr(data, "theory_type") == "Chi-square test of indep"){
174+
else if(
175+
attr(data, "theory_type") %in%
176+
c("Chi-square test of indep", "Chi-square Goodness of Fit")){
174177
infer_plot <- both_chisq_plot(data = data,
175178
deg_freedom = attr(data, "distr_param"),
176179
statistic_text = "Chi-Square",
@@ -218,7 +221,7 @@ theory_t_plot <- function(deg_freedom, statistic_text = "t",
218221
both_t_plot <- function(data, deg_freedom, statistic_text = "t",
219222
dens_color = "black",
220223
obs_stat = NULL,
221-
direction = NULL, bins = 30,...){
224+
direction = NULL, bins = 15,...){
222225
# infer_t_plot <- ggplot(data = data, mapping = aes(x = stat))
223226

224227
infer_t_plot <- shade_density_check(data = data, #gg_plot = infer_t_plot,
@@ -252,7 +255,7 @@ both_anova_plot <- function(data, deg_freedom_top,
252255
deg_freedom_bottom, statistic_text = "F",
253256
dens_color = "black",
254257
obs_stat = NULL,
255-
direction = NULL, bins = 30,....){
258+
direction = NULL, bins = 15,....){
256259

257260
infer_anova_plot <- shade_density_check(data = data,
258261
obs_stat = obs_stat,
@@ -280,7 +283,7 @@ theory_z_plot <- function(statistic_text = "z", dens_color = "black", ...){
280283
both_z_plot <- function(data, statistic_text = "z",
281284
dens_color = "black",
282285
obs_stat = NULL,
283-
direction = NULL, bins = 30,...){
286+
direction = NULL, bins = 15,...){
284287
infer_z_plot <- shade_density_check(data = data,
285288
obs_stat = obs_stat,
286289
direction = direction,
@@ -309,7 +312,7 @@ theory_chisq_plot <- function(deg_freedom, statistic_text = "Chi-Square",
309312
both_chisq_plot <- function(data, deg_freedom, statistic_text = "Chi-Square",
310313
dens_color = "black",
311314
obs_stat = NULL,
312-
direction = "greater", bins = 30,...){
315+
direction = "greater", bins = 15,...){
313316

314317
infer_chisq_plot <- shade_density_check(data = data,
315318
obs_stat = obs_stat,

implement_new_methods.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ chunks of `*both_methods.Rmd` file there up to "Implemented"
1111
- Add undefined global functions or variables to `infer.R` `globalVariables`
1212
- Run `devtools::check()` again until 0 errors | 0 warnings | 0 notes
1313
1. Run `pkgdown::build_site()`
14-
1. Commit files to GitHub and continue
14+
1. Commit files to GitHub and continue
15+

man/visualize.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-calculate.R

+17
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ test_that("chi-square matches chisq.test value", {
175175
dplyr::do(broom::tidy(stats::chisq.test(table(.$Species)))) %>%
176176
dplyr::select(replicate, stat = statistic)
177177
expect_equal(infer_way, trad_way)
178+
179+
gen_iris9a <- iris %>%
180+
specify(Species ~ NULL) %>%
181+
hypothesize(null = "point",
182+
p = c("setosa" = 0.8,
183+
"versicolor" = 0.1,
184+
"virginica" = 0.1)) %>%
185+
generate(reps = 10, type = "simulate")
186+
infer_way <- calculate(gen_iris9a, stat = "Chisq")
187+
#chisq.test way
188+
trad_way <- gen_iris9a %>%
189+
dplyr::group_by(replicate) %>%
190+
dplyr::do(broom::tidy(stats::chisq.test(table(.$Species),
191+
p = c(0.8, 0.1, 0.1)))) %>%
192+
dplyr::select(replicate, stat = statistic)
193+
expect_equal(infer_way, trad_way)
194+
178195
})
179196

180197
test_that("`order` is working", {

0 commit comments

Comments
 (0)