Nothing
#' Goodness of fit test
#'
#' @description
#' This function is for the goodness of fit test of one categorical variable
#'
#' @param x an observed frequency vector
#' @param p probability of each group
#' @param conf.level confidence level for testing hypothesis, default is \code{0.95}, user can change to \code{0.90} or otherwise
#'
#' @importFrom stats pchisq qchisq
#'
#' @return output for the goodness of fit test
#' @export
#'
#' @references
#' Chernoff, H. and Lehmann, E.L. (1954) The Use of Maximum Likelihood Estimates in Chi-Square Tests for Goodness of Fit.
#' Annals of Mathematical Statistics, 25, 579-586. <doi.org/10.1214/aoms/1177728726>.
#'
#' @examples
#' x=c(12,9,10,7,12)
#' prob=c(1/5,1/5,1/5,1/5,1/5) #1:1:1:1:1
#' gofchisq(x=x,p=prob)
#'
gofchisq <- function(x,p,conf.level=0.95){
k <- length(x)
n <- sum(x)
Observed <- as.vector(x) #O[i]
Expected <- as.vector(n*p) #E[i]
if(k>2){
Resi <-Observed-Expected
}else{
Resi <-abs(Observed-Expected)-0.5
}
Resisquare <- Resi^2
Chisqvalue <- as.vector(Resisquare/Expected)
X_squared <- sum(Chisqvalue)
dfchi <- k-1
P_value<- pchisq(X_squared,df=dfchi,lower.tail = FALSE)
Alpha <- 1-conf.level
C_value <- qchisq(1-Alpha,df=dfchi)
result_table=data.frame(O=x,p=signif(p,2),E=Expected,Res=Resi,
ResSq=Resisquare,Chi_value=signif(Chisqvalue,4))
cat("\t\t Chi-squared test for given probabilities\n")
cat("\n",paste("data:","x"),"\n")
cat("X-squared = ",signif(X_squared,4)," ","df = ",dfchi," ",
"Critical-value = ",signif(C_value,4)," ",
"p-value = ",signif(P_value,4),"\n")
cat("*** Output for calculate X-square ***\n")
return(result_table)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.