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)
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.
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")
# 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.
# 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