library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
Gem <- read_excel("C:/Stat50-Final exam/DataFinalExam.xlsx")
Gem
## # A tibble: 163 × 33
##      Age Gender `Course Taken`     In1   In2   In3   In4   In5   In6   In7   In8
##    <dbl> <chr>  <chr>            <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1    22 Female BS Mathematics       4     3     2     1     4     7     6     7
##  2    23 Female BS Biology           6     6     4     4     4     5     4     7
##  3    20 Female BSED English         5     5     3     3     2     6     5     7
##  4    22 Female BSED Biology         4     5     4     3     3     6     6     7
##  5    23 Male   BSED English         7     6     5     5     4     6     4     7
##  6    22 Female BSED Biology         6     6     6     6     6     7     7     7
##  7    20 Male   BS Civil Engine…     4     5     6     2     5     7     4     1
##  8    21 Female BS Electrical E…     5     6     5     6     5     7     6     7
##  9    21 Female BS Mathematics       6     7     5     5     5     7     7     7
## 10    22 Male   BS Biology           6     7     5     6     7     7     7     7
## # ℹ 153 more rows
## # ℹ 22 more variables: Ex1 <dbl>, Ex2 <dbl>, Ex3 <dbl>, Ex4 <dbl>, Ex5 <dbl>,
## #   Ex6 <dbl>, Ex7 <dbl>, Ex8 <dbl>, Ex9 <dbl>, Ex10 <dbl>, Ex11 <dbl>,
## #   TP1 <dbl>, TP2 <dbl>, TP3 <dbl>, TP4 <dbl>, TP5 <dbl>, T6 <dbl>, CP1 <dbl>,
## #   CP2 <dbl>, CP3 <dbl>, CP4 <dbl>, CP5 <dbl>
 1.1 First output:
Gem1 <- Gem %>%
  group_by(`Course Taken`) %>%
  summarise(Frequency = n(),
            'Mean Age' = round(mean(`Age`), 1),
            'SD of Age' = round(sd(`Age`), 3))

Gem1
## # A tibble: 6 × 4
##   `Course Taken`            Frequency `Mean Age` `SD of Age`
##   <chr>                         <int>      <dbl>       <dbl>
## 1 BS Biology                       33       21.6       0.751
## 2 BS Civil Engineering             16       21.4       0.727
## 3 BS Electrical Engineering        17       21.6       0.618
## 4 BS Mathematics                   33       21.7       0.924
## 5 BSED Biology                     32       21.5       0.803
## 6 BSED English                     32       21.6       0.878

1.2 Second output:

1.2 Second output: Consider the variables: In4, Ex3, TP3, and CP3 that are grouped according to “course taken” Mean Intrinsic4 is the average for the varianble “In4” Mean Extrinsic3 is the average for the varianble “Ex3” Mean TP3 is the average of the variable “TP3” Mean CP3 is the average of the variable “CP3”

Gem2<-Gem%>%
  group_by(`Course Taken`) %>%
  summarise(Frequency = n(),
            `Mean Intrinsic4` = mean(In4),
            `Mean Extrinsic3` = mean(Ex3),
            `Mean TP3` = mean(TP3),
            `Mean CP3` = mean(CP3))
Gem2
## # A tibble: 6 × 6
##   `Course Taken`        Frequency `Mean Intrinsic4` `Mean Extrinsic3` `Mean TP3`
##   <chr>                     <int>             <dbl>             <dbl>      <dbl>
## 1 BS Biology                   33              4.94              5.27       3.88
## 2 BS Civil Engineering         16              4.06              5.5        3.31
## 3 BS Electrical Engine…        17              4.35              5          3.47
## 4 BS Mathematics               33              4.27              5.39       3.55
## 5 BSED Biology                 32              4.34              5.22       3.25
## 6 BSED English                 32              4.19              5.66       3.91
## # ℹ 1 more variable: `Mean CP3` <dbl>

1.3 Please recode the responses in the variables “In3” and “In4” with the following changes:

“1 for”Strong Disagree” “2” for “Disagree” “3” for “Moderately Disagree” “4” for “Neutral” “5” for “Moderately Agree” “6” for “Agree” “7” for “Strongly Agree”

R2<-Gem%>%
  mutate(In3.1=recode(`In3`, 
                          "1" = "Strongly Disagree", "2" ="Disagree", "3" = "Moderately Disagree", "4" = "Neutral", "5"="Agree", "6" = "Strongly Agree", "7" = "Moderately Agree"))%>%
  mutate(In4.1=recode(`In4`, 
                          "1" = "Strongly Disagree", "2" ="Disagree", "3" = "Moderately Disagree", "4" = "Neutral", "5"="Agree", "6" = "Strongly Agree", "7" = "Moderately Agree"))
Gem$In1=as.numeric(Gem$In1)
Gem$In2=as.numeric(Gem$In2)
Gem$In3=as.numeric(Gem$In3)
Gem$In4=as.numeric(Gem$In4)
Gem$In5=as.numeric(Gem$In5)

2.1 Answer the following:

a. How many observations are there where “In3” is “strongly agree” and “In4” is “moderately agree”?

Gem2<-R2%>%
  filter(In3.1=="Strongly Agree")%>%
  group_by(In3.1, In4.1) %>%
  summarise(count=n())
## `summarise()` has grouped output by 'In3.1'. You can override using the
## `.groups` argument.
Gem2
## # A tibble: 5 × 3
## # Groups:   In3.1 [1]
##   In3.1          In4.1            count
##   <chr>          <chr>            <int>
## 1 Strongly Agree Agree                9
## 2 Strongly Agree Disagree             2
## 3 Strongly Agree Moderately Agree     3
## 4 Strongly Agree Neutral              7
## 5 Strongly Agree Strongly Agree      12

Answer: There are only 3 observation in variable In3 that are strongly agree at the same time moderately disagree in In4.

How many observations are there where “In3” is “strongly agree” and “In4” is “Neutral”?

Gem3<-R2%>%
  filter(In3.1=="Strongly Agree", In4.1=="Neutral")%>%
  group_by(In3.1, In4.1) %>%
  summarise(count=n())
## `summarise()` has grouped output by 'In3.1'. You can override using the
## `.groups` argument.
Gem3
## # A tibble: 1 × 3
## # Groups:   In3.1 [1]
##   In3.1          In4.1   count
##   <chr>          <chr>   <int>
## 1 Strongly Agree Neutral     7

Answer: There are 7 observations in variable In3 that are strongly agree at the same time Neutral in In4.

3. Consider the following:

Make a new variable named as “InAverage”, InAverage is the average of the responses in the variables In1, In2, In3, In4, and In5.

# Calculating the average of In1, In2, In3, In4, and In5
Gem$InAverage <- rowMeans(Gem[, c("In1", "In2", "In3", "In4", "In5")])

Make two groups of the variable “Course Taken” Grouping: Group 1: BS Civil Engineering, BS Electrical Engineering, ad BS Mathematics Group 2: BS Biology, BSED Biology, and BSED English

# Grouping the Course Taken variable
Group1 <- c("BS Civil Engineering", "BS Electrical Engineering", "BS Mathematics")
Group2 <- c("BS Biology", "BSED Biology", "BSED English")

Gem$Group <- ifelse(Gem$`Course Taken` %in% Group1, "Group 1", "Group 2")

3.1 Is there a significant difference between the two groups of “age”Course Taken” in terms of the variable “InAverage”? (Note: 1.1 Check first the equality of variances 1.2 Answer this by including all the steps in hypothesis testing)

# Grouping the Course Taken variable
Group1 <- c("BS Civil Engineering", "BS Electrical Engineering", "BS Mathematics")
Group2 <- c("BS Biology", "BSED Biology", "BSED English")

Gem$Group <- ifelse(Gem$`Course Taken` %in% Group1, "Group 1", "Group 2")

Checking the equality of Variances

# Independent samples t-test
t_test <- t.test(InAverage ~ Group, data = Gem, var.equal = TRUE)
t_test
## 
##  Two Sample t-test
## 
## data:  InAverage by Group
## t = -0.050692, df = 161, p-value = 0.9596
## alternative hypothesis: true difference in means between group Group 1 and group Group 2 is not equal to 0
## 95 percent confidence interval:
##  -0.3582544  0.3403225
## sample estimates:
## mean in group Group 1 mean in group Group 2 
##              4.784848              4.793814

Null hypothesis: There is no significant difference between the two groups of courses taken in terms of the variable “InAverage”

Alternative hypothesis: There is a significant difference between the two groups of courses taken in terms of the variable “InAverage”

Interpretation: It shows that there is no significant difference between the two groups of courses taken in terms of the variable “InAverage” since the p-value is 0.2458 which exceeds the level of significance (0.05). Therefore, null hypothesis is accepted.

4 Is there a significant difference among the courses taken in terms of the variable “InAverage”(Refer to Q3 for the “Inaverage” variable)? (Note: 1. Answer this by using F-test 1.2 Answer this by including all the steps in hypothesis testing 1.3 Provide results for pairwise comparison if significant)

# Fit ANOVA model
anova_model <- aov(InAverage ~ `Course Taken`, data = Gem)

# Conduct ANOVA F-test
anova_result <- anova(anova_model)

# View ANOVA table
print(anova_result)
## Analysis of Variance Table
## 
## Response: InAverage
##                 Df  Sum Sq Mean Sq F value Pr(>F)
## `Course Taken`   5   1.957 0.39131  0.3137 0.9042
## Residuals      157 195.868 1.24757
# Check column names
names(Gem)
##  [1] "Age"          "Gender"       "Course Taken" "In1"          "In2"         
##  [6] "In3"          "In4"          "In5"          "In6"          "In7"         
## [11] "In8"          "Ex1"          "Ex2"          "Ex3"          "Ex4"         
## [16] "Ex5"          "Ex6"          "Ex7"          "Ex8"          "Ex9"         
## [21] "Ex10"         "Ex11"         "TP1"          "TP2"          "TP3"         
## [26] "TP4"          "TP5"          "T6"           "CP1"          "CP2"         
## [31] "CP3"          "CP4"          "CP5"          "InAverage"    "Group"
# Check structure of dataframe
str(Gem)
## tibble [163 × 35] (S3: tbl_df/tbl/data.frame)
##  $ Age         : num [1:163] 22 23 20 22 23 22 20 21 21 22 ...
##  $ Gender      : chr [1:163] "Female" "Female" "Female" "Female" ...
##  $ Course Taken: chr [1:163] "BS Mathematics" "BS Biology" "BSED English" "BSED Biology" ...
##  $ In1         : num [1:163] 4 6 5 4 7 6 4 5 6 6 ...
##  $ In2         : num [1:163] 3 6 5 5 6 6 5 6 7 7 ...
##  $ In3         : num [1:163] 2 4 3 4 5 6 6 5 5 5 ...
##  $ In4         : num [1:163] 1 4 3 3 5 6 2 6 5 6 ...
##  $ In5         : num [1:163] 4 4 2 3 4 6 5 5 5 7 ...
##  $ In6         : num [1:163] 7 5 6 6 6 7 7 7 7 7 ...
##  $ In7         : num [1:163] 6 4 5 6 4 7 4 6 7 7 ...
##  $ In8         : num [1:163] 7 7 7 7 7 7 1 7 7 7 ...
##  $ Ex1         : num [1:163] 4 4 5 5 7 7 7 7 7 5 ...
##  $ Ex2         : num [1:163] 3 6 5 5 7 7 7 7 6 6 ...
##  $ Ex3         : num [1:163] 4 5 5 6 7 6 6 7 6 7 ...
##  $ Ex4         : num [1:163] 6 4 6 6 7 5 6 7 6 7 ...
##  $ Ex5         : num [1:163] 5 4 7 6 7 6 7 7 6 7 ...
##  $ Ex6         : num [1:163] 4 4 5 6 7 7 7 7 6 7 ...
##  $ Ex7         : num [1:163] 4 6 5 6 7 7 7 6 7 7 ...
##  $ Ex8         : num [1:163] 3 6 5 6 7 7 6 6 7 7 ...
##  $ Ex9         : num [1:163] 3 6 5 6 7 6 6 6 7 7 ...
##  $ Ex10        : num [1:163] 3 6 5 6 7 6 6 6 7 7 ...
##  $ Ex11        : num [1:163] 5 6 5 5 7 7 6 7 6 7 ...
##  $ TP1         : num [1:163] 7 4 6 7 1 4 2 4 3 7 ...
##  $ TP2         : num [1:163] 5 6 5 6 1 6 2 4 5 7 ...
##  $ TP3         : num [1:163] 1 4 3 6 1 5 5 3 3 6 ...
##  $ TP4         : num [1:163] 7 5 3 6 2 5 4 3 2 6 ...
##  $ TP5         : num [1:163] 4 4 3 6 4 5 2 3 5 6 ...
##  $ T6          : num [1:163] 6 5 5 6 1 7 5 4 7 7 ...
##  $ CP1         : num [1:163] 6 7 5 6 6 6 5 5 5 7 ...
##  $ CP2         : num [1:163] 4 5 5 6 1 6 1 5 5 7 ...
##  $ CP3         : num [1:163] 6 2 2 6 1 5 4 7 1 7 ...
##  $ CP4         : num [1:163] 1 2 2 5 1 2 4 1 3 5 ...
##  $ CP5         : num [1:163] 2 1 1 2 1 1 1 1 1 5 ...
##  $ InAverage   : num [1:163] 2.8 4.8 3.6 3.8 5.4 6 4.4 5.4 5.6 6.2 ...
##  $ Group       : chr [1:163] "Group 1" "Group 2" "Group 2" "Group 2" ...

Null hypothesis: There is no significant difference among the courses taken in terms of InAverage.

Alternative hypothesis: There is a significant difference among the courses taken in terms of InAverage.

Interpretation: It shows that there is no significant difference among the courses taken in terms of the variable “InAverage” since the p-value is 0.904 which exceeds the 0.05 significance level. Therefore, null hypothesis is accepted.

5.Given that you get the strength of association between the variables In3 and In4, what is the strenght of association using Pearson Correlation?

# Load the dataset
Gem <- read_excel("C:/Stat50-Final exam/DataFinalExam.xlsx")
Gem
## # A tibble: 163 × 33
##      Age Gender `Course Taken`     In1   In2   In3   In4   In5   In6   In7   In8
##    <dbl> <chr>  <chr>            <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1    22 Female BS Mathematics       4     3     2     1     4     7     6     7
##  2    23 Female BS Biology           6     6     4     4     4     5     4     7
##  3    20 Female BSED English         5     5     3     3     2     6     5     7
##  4    22 Female BSED Biology         4     5     4     3     3     6     6     7
##  5    23 Male   BSED English         7     6     5     5     4     6     4     7
##  6    22 Female BSED Biology         6     6     6     6     6     7     7     7
##  7    20 Male   BS Civil Engine…     4     5     6     2     5     7     4     1
##  8    21 Female BS Electrical E…     5     6     5     6     5     7     6     7
##  9    21 Female BS Mathematics       6     7     5     5     5     7     7     7
## 10    22 Male   BS Biology           6     7     5     6     7     7     7     7
## # ℹ 153 more rows
## # ℹ 22 more variables: Ex1 <dbl>, Ex2 <dbl>, Ex3 <dbl>, Ex4 <dbl>, Ex5 <dbl>,
## #   Ex6 <dbl>, Ex7 <dbl>, Ex8 <dbl>, Ex9 <dbl>, Ex10 <dbl>, Ex11 <dbl>,
## #   TP1 <dbl>, TP2 <dbl>, TP3 <dbl>, TP4 <dbl>, TP5 <dbl>, T6 <dbl>, CP1 <dbl>,
## #   CP2 <dbl>, CP3 <dbl>, CP4 <dbl>, CP5 <dbl>
# Calculate the Pearson correlation between two variables
pearson_correlation <- cor(Gem$In3, Gem$In4)

# Print the Pearson correlation
pearson_correlation
## [1] 0.6377747

Answer The strenght of association using Pearson Correlation is 0.6377747.

6. ABC Brewery manufactures and distributes three types of beer: low calorie, regular beer, and dark beer. In n analysis of the market segments of the three beers, the firm’s market research group has raised the question of whether or not preferences for the beers differ between male and female beer drinkers. If beer preference is dependent of the drinker’s gender, then one advertising campaign will be initiated for all ABC beers. However, if beer preference depends on gender, the firm will tailor its promotions toward different target markets. A study survey for this study resulted as follows:

# Create a data frame with the given data, including the total row
beer_data <- data.frame(
  Gender = c("Male", "Female", "Total"),
  Low_Calorie = c(20, 30, 50),
  Regular_Beer = c(40, 30, 70),
  Dark_Beer = c(80, 70, 150),
  Total = c(140, 130, 270)
)

# Print the data frame
beer_data
##   Gender Low_Calorie Regular_Beer Dark_Beer Total
## 1   Male          20           40        80   140
## 2 Female          30           30        70   130
## 3  Total          50           70       150   270
# Conduct a chi-square test of independence
beer_chi_square <- chisq.test(beer_data[,c("Low_Calorie", "Regular_Beer", "Dark_Beer")], beer_data$Gender)

# Print the results
beer_chi_square
## 
##  Pearson's Chi-squared test
## 
## data:  beer_data[, c("Low_Calorie", "Regular_Beer", "Dark_Beer")]
## X-squared = 3.73, df = 4, p-value = 0.4438