setwd("E:\\S9510\\CAP3330\\CAP3330_Summer2024")
Golf_df = read.csv("Golf.csv",colClasses = c("numeric","factor","factor"))
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## cols = 2 != length(data) = 3
Golf_df

ENTER YOUR NAME HERE: Arian Pousa

To answer the Exam questions, do the following:

  1. If a question requires you to write R code to answer it, add code chunks and enter the code needed to answer the question.

  2. If a question asks you to interpret/justify/write your answer, proceed to interpret/justify/write in a text section below the question.

THERE ARE 10 QUESTIONS IN THE EXAM.

Question 1:

The US Golf Association wants to compare the mean distance traveled by four different brands of golf balls (brands A, B, C, and D). The following experiment is conducted: 10 balls of each brand are randomly selected. Each ball is struck by a robot, and the distance traveled in yards is recorded. You are being asked to apply ANOVA to analyze the data for the US Golf Association to help them reach a reasonable conclusion. Assume all the assumptions needed for the validity of ANOVA are satisfied.

The data to answer this question can be found on Canvas (inside the Exam section) in the CSV file “Golf”. Read this file as an R data frame using the Import Dataset option from the Environment tab. Remember to click the option “Strings as factors”.

str(Golf_df)
## 'data.frame':    40 obs. of  2 variables:
##  $ distance  : num  260 258 266 262 263 261 264 260 263 259 ...
##  $ golf_brand: Factor w/ 4 levels "A","B","C","D": 1 1 1 1 1 1 1 1 1 1 ...

1 a) Set up the hypotheses relevant to this ANOVA problem.

Answer to question 1a below

Ho: A = B
A = C
A = D
B = C
B = D
C = D
Ha: At least two of the mean distances are different

1 b) Run ANOVA and make the appropriate conclusion

Answer to question 1b below

average_distance <- aov(Golf_df$distance ~ Golf_df$golf_brand)

summary(average_distance)
##                    Df Sum Sq Mean Sq F value Pr(>F)  
## Golf_df$golf_brand  3  120.5   40.16   3.355 0.0294 *
## Residuals          36  430.9   11.97                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The p-value for golf brand is really small (0.0294); therefore, it is less than alpha 0.05. We reject Ho and support Ha. We can claim that the golf brand has a significant effect on the distance the gold ball travels.

1 c) Do a post-hoc analysis using Bonferroni or Tukey (use ONLY ONE of them) and discuss which brands yield significantly different average distance traveled.

Answer to question 1 c below

TukeyHSD(average_distance)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Golf_df$distance ~ Golf_df$golf_brand)
## 
## $`Golf_df$golf_brand`
##     diff        lwr      upr     p adj
## B-A -1.6 -5.7670142 2.567014 0.7307514
## C-A  1.7 -2.4670142 5.867014 0.6925658
## D-A  3.0 -1.1670142 7.167014 0.2302328
## C-B  3.3 -0.8670142 7.467014 0.1621764
## D-B  4.6  0.4329858 8.767014 0.0257226
## D-C  1.3 -2.8670142 5.467014 0.8349424
pairwise.t.test(Golf_df$distance, Golf_df$golf_brand, p.adjust.method = "bonferroni")
## 
##  Pairwise comparisons using t tests with pooled SD 
## 
## data:  Golf_df$distance and Golf_df$golf_brand 
## 
##   A     B     C    
## B 1.000 -     -    
## C 1.000 0.239 -    
## D 0.362 0.031 1.000
## 
## P value adjustment method: bonferroni

The only p-value 0.0257226 < alpha (0.05) is the one between D and B. So, according to the Tukey method, the only difference that is statistically significant is the one between the mean of distance D and B. In this case, not all p-values are less than alpha = 0.05; therefore, some differences are not significant.

The only p-value 0.031 < alpha (0.05) is the one between D and B. So, according to the Bonferroni method, the only difference that is statistically significant is the one between the mean of distance D and B. In this case, not all p-values are less than alpha = 0.05; therefore, some differences are not significant.

Question 2:

The company you work for as a data analyst wants to know if the process behind selling product X is working as designed. The process is supposed to work as follows:

When an order of product X is made, the packing system selects a container to pack the product and sell it. There are four brands of containers and the system is expected to select among the four brands of containers randomly ( i.e., the containers from each brand should have the same chance of being chosen).

You collected data about the containers used to pack 150 products and got the following results: 42 containers belonged to brand 1, 33 containers to brand 2, 35 containers to brand 3, and 40 containers to brand 4.

Is the packing system working as designed?

container_package <- c(42, 33, 35, 40)

probvector <- c(1/4, 1/4, 1/4, 1/4)
chisq.test(container_package, p= probvector)$expected
## [1] 37.5 37.5 37.5 37.5
chisq.test(container_package, p= probvector, correct= FALSE)
## 
##  Chi-squared test for given probabilities
## 
## data:  container_package
## X-squared = 1.4133, df = 3, p-value = 0.7024

JUSTIFY and SHOW your work

Answer to question 2 below

we fail to reject Ho.The data does NOT give us sufficient evidence to support the idea that the packaging system is working as design. Ho: the system selects each brand with equal probability (p = 1/4 each) Ha: at least one brand’s probability differs from 1/4 — i.e., it’s not working as designed.

We fail to reject Ho (χ² = 1.413, df = 3, p = 0.702). Since p > alpha, the data does NOT give us sufficient evidence to conclude that the packing system deviates from equal-probability selection. This means the data does not contradict — and is consistent with — the packaging system working as designed.

Question 3:

One professor teaches the same course using three different learning modalities: on-campus, MDC live, and fully virtual.

She wants to compare the grades (i.e., grade letters: “A”, “B”, “C”, “D”, “F”) across these classes to see if the learning modality influences the students’ performance. She collected the following data:

Run the following code chunk to see the table with the data that the professor collected:

matrix_modality_grades = matrix(c(12, 45, 49, 6, 16, 10, 32, 43, 18, 10, 15, 19, 32, 20, 13), nrow= 3, byrow=T, dimnames= list( c("Fully-virtual", "MDC-Live", "On-campus"),  c("A", "B", "C", "D", "F") ) )

matrix_modality_grades
##                A  B  C  D  F
## Fully-virtual 12 45 49  6 16
## MDC-Live      10 32 43 18 10
## On-campus     15 19 32 20 13

This table shows the count (how many) students achieved each grade letter for the different teaching modalities.

chisq.test(matrix_modality_grades, correct = FALSE)
## 
##  Pearson's Chi-squared test
## 
## data:  matrix_modality_grades
## X-squared = 20.637, df = 8, p-value = 0.008177

3 a) Run the appropriate test to help the professor makes a conclusion about the question that she has. Justify your conclusion.

Answer to question 3a below

3 b) (This part IS WORTH 4 POINTS ONLY) If you found a statistically significant effect/association in 3 a), proceed to evaluate and comment on the strength of this effect/association.

Answer to question 3b below

#install.packages("rcompanion")
library(rcompanion)
cramerV(matrix_modality_grades)
## Cramer V 
##   0.1742
#compute Cramer V by hand
sqrt(chisq.test(matrix_modality_grades, correct=FALSE)$statistic / (sum(matrix_modality_grades) * (min(dim(matrix_modality_grades)) - 1)))
## X-squared 
## 0.1742079
chi_result <- chisq.test(matrix_modality_grades, correct = FALSE)
cramers_v <- sqrt(chi_result$statistic / (sum(matrix_modality_grades) * (min(dim(matrix_modality_grades)) - 1)))
cramers_v
## X-squared 
## 0.1742079

Question 4:

As part of an accreditation process, a local high school in Miami needs to show statistical evidence that the average SAT score of its students is above the national average, which is 1050 points.

The accreditation process requires the schools to take a random sample of 50 SAT scores from the SAT scores obtained by all its students. The sample SAT scores collected by the local high school in Miami are recorded in the vector “sat_miami_hs”. Run the following code chunk to create this vector.

sat_miami_hs= c(1150, 1160, 1110, 1055, 1085, 1120, 980, 1030, 1135, 1020, 1045, 1010, 1030, 1120, 1020, 1130, 1000, 910, 1005, 1080, 1050, 1015, 1010, 1040, 1010, 1010, 965, 1020, 1130, 1045, 985, 1065, 1120, 1155, 955, 1050, 1005, 975, 1140, 1060, 1165, 1180, 1060, 1180, 1080, 995, 1030, 1180, 940, 1100)

4 a) Run the appropriate test that allows the local high school to have statistical evidence of whether they meet (or not) the standard required by the accreditation process.

Before running the test in R, you are required to state the relevant hypotheses (Ho and Ha). After running the test, make the appropriate conclusion.

Ho: avg Miami SAT scores <= avg national SAT scores Ha: avg Miami SAT scores > avg national SAT scores

Answer to question 4a below

t.test(sat_miami_hs, alternative = c("greater"), mu=1050)
## 
##  One Sample t-test
## 
## data:  sat_miami_hs
## t = 0.84625, df = 49, p-value = 0.2008
## alternative hypothesis: true mean is greater than 1050
## 95 percent confidence interval:
##  1041.954      Inf
## sample estimates:
## mean of x 
##    1058.2

we fail to reject Ho.The data does NOT give us sufficient evidence to support the suspicion that the average Miami SAT is greater than 1050 national average.

4 b) (This part IS WORTH 4 POINTS ONLY) Obtain the sample average from the above sample. Does the sample average contradict your conclusion in 4 a? Explain why (why it contradicts it) or why not (why it does NOT contradict it).

Answer to question 4b below

round(mean(sat_miami_hs), 0)
## [1] 1058

The sample average supports the previous conclusion that the average Miami SAT score is the same as the national average score.

Question 5:

This is a Two-Way ANOVA question.

Engineers believe that the level of impurity existing in a chemical product is affected by the temperature and the pressure present when the product is being produced. To find out if the data support their belief, they collected data for the level of impurity observed at each combination of temperature and pressure.

The data for this question can be found in the data frame “Exam_q5_df”, which will be created once you run the following code chunk:

temperature_q5= rep (c ("100", "125"), each= 20)

pressure_q5 = rep (c ("25", "30", "35", "40"), each= 5, times=2)

impurity_level= c (5, 4, 4, 3, 6, 9, 8, 8, 6, 8, 10, 10, 12, 9, 8, 9, 8, 8, 9, 10, 4, 3, 4,5,4,11, 10, 12, 13, 13, 12, 14, 11, 6, 7, 9, 11, 12, 16, 12)

Exam_q5_df= data.frame (temperature_q5, pressure_q5, impurity_level, stringsAsFactors = TRUE)

5 a) Identify:

The outcome variable —-> impurity level The factors ———-> pressure and temperature The levels of each factor pressure —–> 4 levels “25” “30” “35” “40” temperature —-> 2 levels “100” “125”

levels(Exam_q5_df$pressure_q5)
## [1] "25" "30" "35" "40"
levels(Exam_q5_df$temperature_q5)
## [1] "100" "125"

Also, determine how many treatments there are in this problem.

4 * 2 = 8 treatments

Answer to question 5a below

The outcome variable —-> impurity level The factors ———-> pressure and temperature The levels of each factor pressure —–> 4 levels “25” “30” “35” “40” temperature —-> 2 levels “100” “125”

5 b) (This part IS WORTH 4 POINTS ONLY) Run the following code chunk to get the interaction plot for this exercise:

interaction.plot(Exam_q5_df$pressure_q5, Exam_q5_df$temperature_q5, Exam_q5_df$impurity_level, xlab ="Pressure", trace.label= "Temperature", ylab= "Impurity Level")

Explain WHY this plot clearly shows the presence of interaction in this problem

Note: Stating that “the interaction is evident because the lines are not parallel” (or something similar) does NOT count as a valid explanation.

Answer to question 5b below

The lines for each temperature are not parallel. Instead, they show a pattern where the purity level changes differently for each type of pressure increase. For temperature of 125, the increase in purity is noticeable between 25 and 30 pressure and then it stays constant. For 100 degrees, there is a noticeable increase between 25 and 35 pressure but then it begins to decline after. This indicates that the effect of pressure on impurity levels is dependent on the temperature.

5 c) Run an ANOVA test and find something from its output that confirms your explanation in 5b). In other words, find something from the ANOVA output that confirms that there is a statistically significant interaction in this problem. EXPLAIN.

Answer to question 5c below

impurity_anova= aov(impurity_level ~ pressure_q5 * temperature_q5, data=Exam_q5_df)

summary(impurity_anova)
##                            Df Sum Sq Mean Sq F value   Pr(>F)    
## pressure_q5                 3 257.27   85.76  26.799 7.36e-09 ***
## temperature_q5              1  30.63   30.63   9.570  0.00408 ** 
## pressure_q5:temperature_q5  3  35.48   11.83   3.695  0.02167 *  
## Residuals                  32 102.40    3.20                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The p-value for pressure is small (7.36e-09); therefore, it is less than alpha 0.05. We reject Ho and support Ha. We can claim that at least two of the pressures have significant individual effects on the impurity levels.

The p-value for temperature is really small (0.00408); therefore, it is less than alpha 0.05. We reject Ho and support Ha. We can claim that both temperatures have significant individual effects on the impurity levels.

The p-value for the interaction is 0.02167, smaller than alpha 0.05. Therefore, we do reject Ho and support Ha in case of the interaction effect. The data gives us evidence to claim that the effect of pressure’s and temperature have significant individual effects on the impurity levels.The data gives us evidence to claim that there is an interaction between pressure and the temperature on the impurity levels.

Question 6:

A meteorologist wants to compare the average maximum temperature in Summer between Des Moines and Spokane, both cities in Washington State. The meteorologist suspects that, on average, Des Moines is hotter than Spokane.

The meteorologist collected the following data (run the following code chunk to create vectors with the maximum temperature values for 10 days in each city):

des_moines_temp= c(86, 91, 94, 89, 89, 96, 91, 92, 90, 95)

spokane_temp = c(88, 82, 91, 87, 89, 91, 90, 91, 92, 93)

Assume that the temperature data follow a Normal distribution and have the same standard deviation for both populations. Use a significance level of 0.05.

6 a) Set up the appropriate hypotheses (Ho and Ha) that the meteorologist should put forward to conduct this study.

Answer to question 6a below

ho: Des Moines <= Spokane ha: Des Moines > Spokane

t.test(des_moines_temp,spokane_temp,alternative = c("greater"), var.equal=TRUE)
## 
##  Two Sample t-test
## 
## data:  des_moines_temp and spokane_temp
## t = 1.3645, df = 18, p-value = 0.09461
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
##  -0.5145794        Inf
## sample estimates:
## mean of x mean of y 
##      91.3      89.4

6 b) Run the appropriate test to help the meteorologist find out if her suspicion is supported by the data. Make the appropriate conclusion and justify.

Answer to question 6b below

The PV (0.09461) is greater than the significance level (alpha= 0.05); therefore, fail to reject Ho and support Ha.

the data is not backing up the suspicion that the summer temperature is higher on average in Des Moines.

Question 7:

The data frame you are going to use to answer questions from 7 to 10 is called “Auto”. It contains data about gas mileage, horsepower, and other information for 392 vehicles. This data frame is part of the ISLR package.

You already installed the ISLR package (because we used in class to do the Credit example and also for assignment 6). Load the ISLR package by running the following code chunk:

library (ISLR)

Then, get familiar with the Auto data frame by running the following code chunk:

str (Auto)
## 'data.frame':    392 obs. of  9 variables:
##  $ mpg         : num  18 15 18 16 17 15 14 14 14 15 ...
##  $ cylinders   : num  8 8 8 8 8 8 8 8 8 8 ...
##  $ displacement: num  307 350 318 304 302 429 454 440 455 390 ...
##  $ horsepower  : num  130 165 150 150 140 198 220 215 225 190 ...
##  $ weight      : num  3504 3693 3436 3433 3449 ...
##  $ acceleration: num  12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
##  $ year        : num  70 70 70 70 70 70 70 70 70 70 ...
##  $ origin      : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ name        : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...

Conduct a regression analysis using the variable called “mpg” (miles per gallon) as the dependent variable and the variable called “horsepower” as the predictor variable. Answer the following questions:

7 a) Evaluate the statistical significance of the equation.

Answer to question 7a below

MPG_R = lm(mpg ~ horsepower, data=Auto)

summary(MPG_R)
## 
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5710  -3.2592  -0.3435   2.7630  16.9240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 39.935861   0.717499   55.66   <2e-16 ***
## horsepower  -0.157845   0.006446  -24.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.6049 
## F-statistic: 599.7 on 1 and 390 DF,  p-value: < 2.2e-16

The PV for the b1 coefficient is very small (PV < 2e-16). This PV is less than alpha. Therefore, we can reject Ho and support Ha. The data give us evidence to conclude that there is a statistically significant relationship between horse power and mpg.

7 b) Is more horsepower related to more mpg or to less mpg? Justify by using the output of the regression analysis (You cannot use anything else but the output of the regression analysis to justify).

Answer to question 7b below

39.935861 - 0.157845*130
## [1] 19.41601
39.935861 - 0.157845*150
## [1] 16.25911

No, horsepower has a negative relationship to mpg. The more horse power the less mpg in an automobile

Question 8

With reference to the regression you did in question 7. Do a plot of the residuals versus the predicted values (i.e., residuals in the Y axis and the predicted values on X). What does the pattern that you observe tell you about the validity of assumption 1 ? EXPLAIN

Answer to question 8 below

plot(predict(MPG_R), residuals(MPG_R), xlab = "Predict", ylab="Residuals")

abline(h=0, col = "red")

The plot is showing some outliers and the residuals getting spread in a parabolic shape as the predicted value increase. Therefore, the variability is not the same for all values.

Question 9:

Does the equation you obtained in question 7 improve by adding “acceleration” as a second predictor?

Answer this question using Adjusted R Squared and a lower bound of 3% to evaluate the percentage change. Show all your work

Answer to question 9 below

(summary(lm(mpg ~ horsepower + acceleration, data=Auto))$adj.r.squared -  summary(lm(mpg ~ horsepower, data=Auto))$adj.r.squared) / summary(lm(mpg ~ horsepower, data=Auto))$adj.r.squared
## [1] 0.03884709

Yes, the new equation with “acceleration” improves using a 3% lower bound as a minimum requirement. The adjusted R squared for the second equation is 3% larger than the first equation and therefore does meet the at least 3% increase minimum to be considered better.

Question 10:

Conduct a regression analysis using the variable called “mpg” (miles per gallon) as the dependent variable and the variable called “weight” as the predictor variable (use “weight” as the only predictor !).

Decide whether the quality of this equation is good or not, based on the following criterion: the RSE of the equation is at most 15% of the average mpg. SHOW YOUR WORK (and remember to answer whether the equation is good or not)

Answer to question 10 below

MPG_R1 = lm(mpg ~ weight, data=Auto)

summary(MPG_R1)
## 
## Call:
## lm(formula = mpg ~ weight, data = Auto)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11.9736  -2.7556  -0.3358   2.1379  16.5194 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 46.216524   0.798673   57.87   <2e-16 ***
## weight      -0.007647   0.000258  -29.64   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.333 on 390 degrees of freedom
## Multiple R-squared:  0.6926, Adjusted R-squared:  0.6918 
## F-statistic: 878.8 on 1 and 390 DF,  p-value: < 2.2e-16
#Coefficient of variation based on RSE
(summary(MPG_R1)$sigma/mean(Auto$mpg))*100
## [1] 18.4796

We can claim that the RSE is not low enough because the coefficient of variation is 18.5% and not below 15%. Thus, the practical significance (prediction quality, the performance) of the equation is not as good as we wanted.