# Roger Palay copyright 2021-04-12 # Saline, MI 48176 # # in the goodfit function: # items is a list of the item values (i.e., labels) # H_null is a list of the hypothesized proportions # observed is a list of the observed counts of items # sig_level is the level at which the test is to be run # show_table if TRUE will cause the function to use # View to display a nice table of values goodfit <- function( items, H_null, observed, sig_level,show_table=FALSE ) { # there are a few checks that we should make # first, is the sum of the H_null proportions == 1 sum_H0 <- sum( H_null ) if( abs( sum_H0 - 1) > 0.00000001) { return("H_null proportions must add to 1")} # second, all of the H_null values must be non-negative find_negs <- which( H_null < 0 ) if( length( find_negs ) > 0 ) {return( "cannot have negative proportions")} # Third, we need the same number of items, # H_null proportions, and observed frequencies items_length <- length( items ) H0_length <- length( H_null ) obs_length <- length( observed ) if( items_length != H0_length | items_length != obs_length) { return("Need same number of items, proportions, and observed frequencies")} obs_total <- sum(observed) expected <- H_null*obs_total # Fourth, we need enough expected values num_too_small <- length( which( expected < 5)) if( num_too_small > 0 ) { cat( "expected values, some too small","\n") cat(expected,"\n") return("use larger sample")} diff <- observed - expected diff2 <- diff^2 chi_component <- diff2/expected total = sum( chi_component ) goff_table <<- data.frame( items, H_null, observed, expected, diff, diff2, chi_component) colnames( goff_table ) <- c( "Items","H_null", "observed", "expected", "diff", "diff^2", "chi component") print( goff_table) if( show_table ) {View( goff_table )} cat( "-----------------------------------\n") cat("total observations = ",obs_total,"\n") cat("Number of categories = ", obs_length,"\n") cat("Number of degrees of freedom = ", (obs_length-1),"\n") cat("Total of chi component =", total,"\n") cat("P(total >= ",total,") = ", pchisq(total, obs_length-1,lower.tail=FALSE), "\n") crit_value <- qchisq( sig_level, obs_length-1, lower.tail=FALSE) cat("For ",(sig_level*100),"% to the right, the ","\n") cat(" critical value is ", crit_value, "\n") if( total > crit_value ) { "reject H0"} else {" not enough evidence to reject H0"} }