setwd("/Users/matthewlehnert/Documents/AUI/Quantitative Methods Code") #this sets the working
directory set.seed(12051984) #this randomly sets a seed xbar_1 <- 82 #sample mean of sample one xbar_2 <- 78 #sample mean of sample two d0 <- 0 #assumption sigma_1 <- 10 #population standard deviation of population one sigma_2 <- 10 #population standard deviation of population two n_1 <- 30 #sample size of sample one n_2 <- 40 #sample size of sample two se <- sqrt(((sigma_1^2)/n_1) + ((sigma_2^2)/n_2)) #standard error z <- ((xbar_1-xbar_2)-d0)/se #calculates Z #calculate 90% critical values using R alpha_10 <- .10 #this is alpha at 90% z_half_alpha_10 <- qnorm(1-(alpha_10/2)) #finds positive z sub alpha over two cv_10 <- c(-z_half_alpha_10, z_half_alpha_10) #saves the negative and positive critical values in a vector #calculate 95% critical values using R alpha_05 <- .05 #this is alpha at 95% z_half_alpha_05 <- qnorm(1-(alpha_05/2)) #finds positive z sub alpha over two cv_05 <- c(-z_half_alpha_05, z_half_alpha_05) #saves the negative and positive critical values in a vector #calculate 99% critical values using R alpha_01 <- .01 #this is alpha at 99% z_half_alpha_01 <- qnorm(1-(alpha_01/2)) #finds positive z sub alpha over two cv_01 <- c(-z_half_alpha_01, z_half_alpha_01) #saves the negative and positive critical values in a vector #calculate the margin of error at 90% moe_90 <- z_half_alpha_10 * se #calculate margin of error CI_90 <- (xbar_1 - xbar_2) + c(-moe_90, moe_90) #finds confidence interval CI_90_est <- print(paste("We are 90% sure that the difference between the population means is between", CI_90[1], "and", CI_90[2])) #prints the confidence interval in a sentence CI_result_90 <- if(d0 <= CI_90[1] | d0 >= CI_90[2]){ print("We are 90% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") }# conducts the hypothesis test #calculate the margin of error at 95% moe_95 <- z_half_alpha_05 * se #calculate margin of error CI_95 <- (xbar_1 - xbar_2) + c(-moe_95, moe_95) #finds confidence interval CI_95_est <- print(paste("We are 95% sure that the difference between the population means is between", CI_95[1], "and", CI_95[2])) #prints the confidence interval in a sentence CI_result_95 <- if(d0 <= CI_95[1] | d0 >= CI_95[2]){ print("We are 95% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") }# conducts the hypothesis test #calculate the margin of error at 99% moe_99 <- z_half_alpha_01 * se #calculate margin of error CI_99 <- (xbar_1 - xbar_2) + c(-moe_99, moe_99) #finds confidence interval CI_99_est <- print(paste("We are 99% sure that the difference between the population means is between", CI_99[1], "and", CI_99[2])) #prints the confidence interval in a sentence CI_result_99 <- if(d0 <= CI_99[1] | d0 >= CI_99[2]){ print("We are 99% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") }# conducts the hypothesis test print(paste(CI_99_est,CI_result_99)) #CONDUCT HYPOTHESIS TEST AT 90% (CRITICAL VALUE) cv_result_90 <- if(z <= -z_half_alpha_10 | z >= z_half_alpha_10){ print("We are 90% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") } #CONDUCT HYPOTHESIS TEST AT 95% (CRITICAL VALUE) cv_result_95 <- if(z <= -z_half_alpha_05 | z >= z_half_alpha_05){ print("We are 95% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") } #CONDUCT HYPOTHESIS TEST AT 99% (CRITICAL VALUE) cv_result_99 <- if(z <= -z_half_alpha_01 | z >= z_half_alpha_01){ print("We are 99% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") } pval <- 2 * pnorm(-abs(z)) pval #CONDUCT HYPOTHESIS TEST AT 90% (p-value) pval_result_90 <- if(pval <= alpha_10){ print("We are 90% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") } #CONDUCT HYPOTHESIS TEST AT 95% (p-value) pval_result_95 <- if(pval <= alpha_05){ print("We are 95% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") } #CONDUCT HYPOTHESIS TEST AT 99% (p-value) pval_result_99 <- if(pval <= alpha_01){ print("We are 99% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") } rm(list=ls()) #this clears the global environment setwd("/Users/matthewlehnert/Documents/AUI/Quantitative Methods Code") #this sets the working directory set.seed(12051984) #this randomly sets a seed xbar <- 23.89 #sample mean mu0 <- 24.57 #assumption sigma <- 2.4 #population standard deviation n <- 30 #sample size se <- sigma/sqrt(n) #standard error z <- (xbar-mu0)/se #calculate 90% critical values using R alpha_10 <- .10 #this is alpha at 90% z_half_alpha_10 <- qnorm(1-(alpha_10/2)) #finds positive z sub alpha over two cv_10 <- c(-z_half_alpha_10, z_half_alpha_10) #saves the negative and positive critical values in a vector #calculate 95% critical values using R alpha_05 <- .05 #this is alpha at 95% z_half_alpha_05 <- qnorm(1-(alpha_05/2)) #finds positive z sub alpha over two cv_05 <- c(-z_half_alpha_05, z_half_alpha_05) #saves the negative and positive critical values in a vector #calculate 99% critical values using R alpha_01 <- .01 #this is alpha at 99% z_half_alpha_01 <- qnorm(1-(alpha_01/2)) #finds positive z sub alpha over two cv_01 <- c(-z_half_alpha_01, z_half_alpha_01) #saves the negative and positive critical values in a vector #calculate the margin of error at 90% moe_90 <- z_half_alpha_10 * se #calculate margin of error CI_90 <- xbar + c(-moe_90, moe_90) #finds confidence interval CI_90_est <- print(paste("We are 90% sure that the population mean is between", CI_90[1], "and", CI_90[2])) #prints the confidence interval in a sentence CI_result_90 <- if(mu0 <= CI_90[1] | mu0 >= CI_90[2]){ print("We are 90% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") }# conducts the hypothesis test
#calculate the margin of error at 95%
moe_95 <- z_half_alpha_05 * se #calculate margin of error CI_95 <- xbar + c(-moe_95, moe_95) #finds confidence interval CI_95_est <- print(paste("We are 95% sure that the population mean is between", CI_95[1], "and", CI_95[2])) #prints the confidence interval in a sentence CI_result_95 <- if(mu0 <= CI_95[1] | mu0 >= CI_95[2]){ print("We are 95% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") }# conducts the hypothesis test
#calculate the margin of error at 99%
moe_99 <- z_half_alpha_01 * se #calculate margin of error CI_99 <- xbar + c(-moe_99, moe_99) #finds confidence interval CI_99_est <- print(paste("We are 99% sure that the population mean is between", CI_99[1], "and", CI_99[2])) #prints the confidence interval in a sentence CI_result_99 <- if(mu0 <= CI_99[1] | mu0 >= CI_99[2]){ print("We are 99% sure that we reject the null hypothesis and accept the alternative hypothesis") } else { print("We fail to reject the null hypothesis") }# conducts the hypothesis test print(paste(CI_99_est,CI_result_99))