#####################################
# File: Lab4.R
# Author: Taeyong Park
# Date Created: Sept 2018
# Summary: R code for Lab Session 4
# Comparing Three or More Groups
######################################
#####################
#
# Population Means
#
#####################
# Chemitech compares three methods used to produce filtration systems.
# To this end, Chemitech measures the number of filtration systems produced per week.
# H0: The three population means are the same.
# Import the data.
chemitech = read.csv("Chemitech.csv")
colnames(chemitech)
View(chemitech)
# Use the stack function to transform the data structure.
stacked.chemitech = stack(chemitech)
View(stacked.chemitech)
# Use the aov function to run an Anova model.
anova.chemitech = aov(values ~ ind, data=stacked.chemitech)
summary(anova.chemitech)
# In the output, 260 is the Between-groups estimate
# and 28.33 is the Within-groups estimate.
######################
# Exercise 1
######################
# Write code for conducting the anova test without using the aov function.
# p value approach
# critical value approach
######################
# Exercise 2
######################
# The Consumer Reports Restaurant Customer Satisfaction Survey
# studies full-service restraurant chanis.
# One of the variables in the study is meal price, the average amount paid per
# person for dinner and drinks, minus the tip.
# The GrandStrand.csv data show the meal prices obtained from 24 restaurants
# in the Grand Strand section in a city of the US.
# Use .05 significance level to test if there is a significant
# difference among the mean meal price for the three types of restraurants.
# Answer the following questions:
# 1. What is the between-groups estimate of population variance?
# 2. What is the within-groups estimate of population variance?
# 3. What is the F statistic?
# 4. What is you conclusion about the difference among the mean meal price for the three types of restraurants?
######################
#
# Population Proportions
#
######################
# Ice-cream preferecne by gender
ice = read.csv("IceCreamPreference.csv")
View(ice)
# To use the chisq.test() function, first create a contingency table.
contingency.ice = table(ice$Preference, ice$Gender)
# Run the analysis and interpret the results.
chisq.test(contingency.ice)
# Critical value
qchisq(0.05, df=2, lower.tail = F)
# p-value
pchisq(6.4468, df=2, lower.tail = F)
############
# Exercise 1
############
# Import AutoLoyalty.csv file.
# Test if the onwers of Impala; Fusion; Accord
# have the same degree of loyalty.
############
# Exercise 2
############
# Write code for conducting the chi-square test without using
# the chisq.test function.
############
# Exercise 3
############
# In a study conducted by Zogby International for the Democrat and Chronicle, more tha 700
# New Yorkers were polled to determine whether the New York state government works.
# Respondents surveyed were asked questions involving pay cuts for state legislators, restrictions on lobbyists,
# terms limits for legislators, and whether state citizens should be able to put
# matters directly on the state ballot for a vote. The results regarding several
# proposed reforms had broad support, crossing all demographic and political lines.
# Suppose that a follow-up survey of 100 individuals who live in the western region of
# New York was conducted. The party affiliation (Democrat, Independent, Republican) of
# each individual surveyed was recorded, as well as the responses to the following
# three questions:
# 1. Should legislative pay be cut for every day the state budget is late? (Yes / No)
# 2. Should there be more restrictions on lobbyists? (Yes / No)
# 3. Should there be term limits requiring that legislators serve a fixed number of years? (Yes / No)
# Import the NYReform.csv data and answer the following questions.
# 1. With regard to question 1, test for the independence of
# the response (Yes and No) and party affiliation. Use sig. level .05.
# 2. With regard to question 2, test for the independence of
# the response (Yes and No) and party affiliation. Use sig. level .05.
# 3. With regard to question 3, test for the independence of
# the response (Yes and No) and party affiliation. Use sig. level .05.