Simple Effects Practice:
Please use the tests you’ve learned from today’s class to analysis the following datasets.
library(readxl)
library(car)
## Loading required package: carData
Kidscalories – children either don’t help (2) or help (1) making dinner and the amount of food they consume is measured. Find which method gets them eating more.
kidsCalories <- read.csv("kidsCalories.csv", header = TRUE)
kidsCalories$helpedinprep <- factor(kidsCalories$helpedinprep)
leveneTest(kidsCalories$calorieintake, kidsCalories$helpedinprep)
t.test(calorieintake~helpedinprep, data = kidsCalories, var.equal = TRUE)
##
## Two Sample t-test
##
## data: calorieintake by helpedinprep
## t = 2.8137, df = 45, p-value = 0.007236
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
## 24.04243 145.15859
## sample estimates:
## mean in group 1 mean in group 2
## 431.3996 346.7991
tapply(kidsCalories$calorieintake, kidsCalories$helpedinprep, sd)
## 1 2
## 105.70124 99.50114
#Children who help in making dinner consume more food
Cholestoraldata – cholesterol levels for the same individuals following a month of using two different brands of margarine. Determine if one lowers cholesterol more than another.
cholestoralData <- read_excel("cholestoralData.xlsx", sheet = 1)
cholestoralData$difference <- cholestoralData$After - cholestoralData$Before
leveneTest(cholestoralData$difference, cholestoralData$Margarine)
## Warning in leveneTest.default(cholestoralData$difference,
## cholestoralData$Margarine): cholestoralData$Margarine coerced to factor.
t.test(difference ~ Margarine, data = cholestoralData, var.equal = FALSE)
##
## Welch Two Sample t-test
##
## data: difference by Margarine
## t = -3.9902, df = 19.854, p-value = 0.0007285
## alternative hypothesis: true difference in means between group A and group B is not equal to 0
## 95 percent confidence interval:
## -5.281831 -1.654169
## sample estimates:
## mean in group A mean in group B
## -3.7805 -0.3125
wilcox.test(cholestoralData$difference~cholestoralData$Margarine)
##
## Wilcoxon rank sum exact test
##
## data: cholestoralData$difference by cholestoralData$Margarine
## W = 86, p-value = 0.001593
## alternative hypothesis: true location shift is not equal to 0
t.test(cholestoralData$Before[cholestoralData$Margarine == "A"], cholestoralData$After[cholestoralData$Margarine == "A"], paired = TRUE)
##
## Paired t-test
##
## data: cholestoralData$Before[cholestoralData$Margarine == "A"] and cholestoralData$After[cholestoralData$Margarine == "A"]
## t = 4.3984, df = 19, p-value = 0.0003089
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 1.981502 5.579498
## sample estimates:
## mean difference
## 3.7805
#Margarine A lowers cholesterol better.
prioritiesData – students’ school priorities based on their location. Determine if there are any differences in priorities by location.
prioritiesData <- read_excel("prioritiesData.xlsx", sheet = 1)
table(prioritiesData$Rural/(sum(prioritiesData$Rural)))
##
## 0.28 0.34 0.38
## 1 1 1
table(prioritiesData$Suburban/(sum(prioritiesData$Suburban)))
##
## 0.14 0.28 0.58
## 1 1 1
table(prioritiesData$Urban/(sum(prioritiesData$Urban)))
##
## 0.14 0.17 0.69
## 1 1 1
chisq.test(prioritiesData[, 2:4])
##
## Pearson's Chi-squared test
##
## data: prioritiesData[, 2:4]
## X-squared = 21.627, df = 4, p-value = 0.0002377
chisq.test(prioritiesData$Rural)
##
## Chi-squared test for given probabilities
##
## data: prioritiesData$Rural
## X-squared = 1.52, df = 2, p-value = 0.4677
chisq.test(prioritiesData$Suburban)
##
## Chi-squared test for given probabilities
##
## data: prioritiesData$Suburban
## X-squared = 30.32, df = 2, p-value = 2.607e-07
chisq.test(prioritiesData$Urban)
##
## Chi-squared test for given probabilities
##
## data: prioritiesData$Urban
## X-squared = 57.38, df = 2, p-value = 3.468e-13
chisq.test(prioritiesData[, 2:3])
##
## Pearson's Chi-squared test
##
## data: prioritiesData[, 2:3]
## X-squared = 9.414, df = 2, p-value = 0.009032
#kids in suburban and urban locations show gretaer priorities for academics than kids in rural locations
VotingData – the order candidates from different parties are listed on the ballot and what candidate is selected. Determine if listing order impacts voting.
VotingData <- read_excel("VotingData.xlsx")
head(VotingData)
DRL <- c(63,310,5)
RLD <- c(69,302,5)
LDR <- c(61,308,5)
chisq.test(data.frame(DRL,RLD,LDR))
## Warning in chisq.test(data.frame(DRL, RLD, LDR)): Chi-squared approximation may
## be incorrect
##
## Pearson's Chi-squared test
##
## data: data.frame(DRL, RLD, LDR)
## X-squared = 0.63124, df = 4, p-value = 0.9595
#There is no significant difference in the proportion of votes for different party candidates by candidate ordering