#Set working directory setwd("~/Dropbox/ISI SCB/Data sets") # read in csv file ovtm <-read.csv("hyp1example4.csv", TRUE) #assign the list of overtime values to the variable x x <- ovtm$overtime #assumed population variance sigmasq <- 25 #sample size n = length(x) n #sample standard deviation s <- sd(x) s #hypotheses #H0: sigmasq = 25 #H1: sigmasq not= 25 #chi-square test statistic ts <- (n-1)*(s^2)/sigmasq ts #lower critical value - 2.5th percentile (0.025th quantile) chi025 <- qchisq(.025,n-1) chi025 #upper critical value - 97.5th percentile (0.975th quantile) chi975 <- qchisq(.975,n-1) chi975 #plot of chi-square density function with #15 degrees of freedom showing test statistic #and the critical values curve(dchisq(x, df = n-1), from = 0, to = 40, main = 'Chi Square Distribution:15 df', ylab = 'Density', xlab = '') abline(v=ts, col = "blue") abline(v=qchisq(.975,df=n-1), col = "red") abline(v=qchisq(.025,df=n-1), col = "red") #determination of the p-value #plot of chi-square density function with #15 degrees of freedom curve(dchisq(x, df = n-1), from = 0, to = 40, main = 'Chi Square Distribution:15 df', ylab = 'Density', xlab = '') #location of the test statistic abline(v=ts,col="blue") # first determination of the value of the density function # for the quantile which is the value of the test statistic ts_density <-dchisq(ts,n-1) ts_density abline(h=ts_density, col="red") #trial and error to determine other qu atile qhich has the same #density function value as the statistic dchisq(ts,n-1) dchisq(9.5,n-1) dchisq(9.6,n-1) dchisq(9.7,n-1) dchisq(9.8,n-1) dchisq(9.9,n-1) dchisq(ts,n-1) dchisq(9.8,n-1) dchisq(9.75,n-1) dchisq(9.76,n-1) dchisq(9.77,n-1) dchisq(9.78,n-1) dchisq(9.79,n-1) dchisq(ts,n-1) dchisq(9.79,n-1) dchisq(9.795,n-1) dchisq(9.796,n-1) dchisq(ts,n-1) dchisq(9.7952,n-1) dchisq(9.7953,n-1) dchisq(ts,n-1) dchisq(9.7952,n-1) dchisq(9.79521,n-1) dchisq(9.79522,n-1) dchisq(9.79523,n-1) dchisq(9.79524,n-1) #after trial and error, arrive at a value of the #quantile of the chi-square distribution with 15 #degrees of freedom which gives approximately the #same value of the density function as the test statistic ts_other = 9.79523 ts_other_density <- dchisq(ts_other,n-1) ts_other_density #location of this quantile abline(v=ts_other,col="green") #horizontal line showing that the density function associated #with the test statistic and ts_other are equal abline(h=ts_density, col="red") #determination of the p-value #P(chisq > ts) pv_up <- pchisq(ts, n-1, lower.tail = FALSE) pv_up #P(chisq < ts_other) pv_lo <- pchisq(ts_other,n-1, lower.tail = TRUE) pv_lo #p-value of the test pvalue = pv_up + pv_lo pvalue #check for outliers boxplot(x) #check for normality of data set qqnorm(x) shapiro.test(x)