#install.packages("dplyr")
#install.packages("readr")
#install.packages("ggplot2")
library(readr)
library(knitr)
library(dplyr)
##
## 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(ggplot2)
Voter<-read_csv("/Users/meiminshan/Desktop/Voter Data 2018.csv")
## Parsed with column specification:
## cols(
## .default = col_double(),
## weight_latino = col_logical(),
## weight_18_24 = col_logical(),
## town_open_2018 = col_character(),
## redovote2016_t_2017 = col_character(),
## job_title_t_2017 = col_character(),
## presvote16post_t_2016 = col_character(),
## second_chance_t_2016 = col_character(),
## race_other_2016 = col_character(),
## healthcov_t_2016 = col_character(),
## employ_t_2016 = col_character(),
## pid3_t_2016 = col_character(),
## religpew_t_2016 = col_character(),
## votemeth16_rnd_2016 = col_character(),
## presvote16post_rnd_2016 = col_character(),
## vote2016_cand2_rnd_2016 = col_character(),
## Clinton_Rubio_rnd_2016 = col_character(),
## Clinton_Cruz_rnd_2016 = col_character(),
## Sanders_Trump_rnd_2016 = col_character(),
## Sanders_Rubio_rnd_2016 = col_character(),
## second_chance_rnd_2016 = col_character()
## # ... with 132 more columns
## )
## See spec(...) for full column specifications.
## Warning: 1319 parsing failures.
## row col expected actual file
## 1424 religpew_muslim_baseline 1/0/T/F/TRUE/FALSE 90 '/Users/meiminshan/Desktop/Voter Data 2018.csv'
## 1537 child_age7_1_baseline 1/0/T/F/TRUE/FALSE 6 '/Users/meiminshan/Desktop/Voter Data 2018.csv'
## 1537 child_age8_1_baseline 1/0/T/F/TRUE/FALSE 4 '/Users/meiminshan/Desktop/Voter Data 2018.csv'
## 1537 child_age9_1_baseline 1/0/T/F/TRUE/FALSE 2 '/Users/meiminshan/Desktop/Voter Data 2018.csv'
## 2958 religpew_muslim_baseline 1/0/T/F/TRUE/FALSE 2 '/Users/meiminshan/Desktop/Voter Data 2018.csv'
## .... ........................ .................. ...... ...............................................
## See problems(...) for more details.
voter<-Voter%>%
mutate(immi_europe = ifelse(immi_region_eur_2018==1, "Increase",
ifelse(immi_region_eur_2018==2, "Keep about the same",
ifelse(immi_region_eur_2018==3, "Decrease", NA))),
immi_middleeast = ifelse(immi_region_mid_2018==1, "Increase",
ifelse(immi_region_mid_2018==2, "Keep about the same",
ifelse(immi_region_mid_2018==3, "Decrease", NA))),
immi_india = ifelse(immi_region_india_2018==1, "Increase",
ifelse(immi_region_india_2018==2, "Keep about the same",
ifelse(immi_region_india_2018==3, "Decrease", NA))),
immi_china = ifelse(immi_region_china_2018==1, "Increase",
ifelse(immi_region_china_2018==2, "Keep about the same",
ifelse(immi_region_china_2018==3, "Decrease", NA))),
immi_africa = ifelse(immi_region_afr_2018==1, "Increase",
ifelse(immi_region_afr_2018==2, "Keep about the same",
ifelse(immi_region_afr_2018==3, "Decrease", NA))),
PartyRegistration = ifelse(partyreg_baseline==1, "Democrat",
ifelse(partyreg_baseline==2, "Republican", NA)),
ft_immig_2017 = ifelse(ft_immig_2017== 997, NA, ft_immig_2017))%>%
select(ft_immig_2017, PartyRegistration,immi_europe,immi_middleeast,immi_india,immi_china,immi_africa)%>%
filter(PartyRegistration%in% c("Democrat", "Republican"))
table(voter$PartyRegistration,voter$immi_europe)%>%
prop.table(1)
##
## Decrease Increase Keep about the same
## Democrat 0.1228571 0.1523810 0.7247619
## Republican 0.2573034 0.1348315 0.6078652
chisq.test(voter$PartyRegistration,voter$immi_europe)
##
## Pearson's Chi-squared test
##
## data: voter$PartyRegistration and voter$immi_europe
## X-squared = 58.02, df = 2, p-value = 2.519e-13
chisq.test(voter$PartyRegistration,voter$immi_europe)[7]
## $expected
## voter$immi_europe
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 193.7629 151.5464 704.6907
## Republican 164.2371 128.4536 597.3093
chisq.test(voter$PartyRegistration,voter$immi_europe)[6]
## $observed
## voter$immi_europe
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 129 160 761
## Republican 229 120 541
table(voter$PartyRegistration,voter$immi_middleeast)%>%
prop.table(1)
##
## Decrease Increase Keep about the same
## Democrat 0.30922931 0.12083730 0.56993340
## Republican 0.74044944 0.02134831 0.23820225
chisq.test(voter$PartyRegistration,voter$immi_middleeast)
##
## Pearson's Chi-squared test
##
## data: voter$PartyRegistration and voter$immi_middleeast
## X-squared = 367.1, df = 2, p-value < 2.2e-16
chisq.test(voter$PartyRegistration,voter$immi_middleeast)[7]
## $expected
## voter$immi_middleeast
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 532.8099 79.05513 439.135
## Republican 451.1901 66.94487 371.865
chisq.test(voter$PartyRegistration,voter$immi_middleeast)[6]
## $observed
## voter$immi_middleeast
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 325 127 599
## Republican 659 19 212
table(voter$PartyRegistration,voter$immi_india)%>%
prop.table(1)
##
## Decrease Increase Keep about the same
## Democrat 0.20821394 0.12798472 0.66380134
## Republican 0.40427928 0.06081081 0.53490991
chisq.test(voter$PartyRegistration,voter$immi_india)
##
## Pearson's Chi-squared test
##
## data: voter$PartyRegistration and voter$immi_india
## X-squared = 97.459, df = 2, p-value < 2.2e-16
chisq.test(voter$PartyRegistration,voter$immi_india)[7]
## $expected
## voter$immi_india
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 312.2062 101.72403 633.0698
## Republican 264.7938 86.27597 536.9302
chisq.test(voter$PartyRegistration,voter$immi_india)[6]
## $observed
## voter$immi_india
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 218 134 695
## Republican 359 54 475
table(voter$PartyRegistration,voter$immi_china)%>%
prop.table(1)
##
## Decrease Increase Keep about the same
## Democrat 0.24144487 0.11406844 0.64448669
## Republican 0.47309417 0.04147982 0.48542601
chisq.test(voter$PartyRegistration,voter$immi_china)
##
## Pearson's Chi-squared test
##
## data: voter$PartyRegistration and voter$immi_china
## X-squared = 127.35, df = 2, p-value < 2.2e-16
chisq.test(voter$PartyRegistration,voter$immi_china)[7]
## $expected
## voter$immi_china
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 365.8189 84.96091 601.2202
## Republican 310.1811 72.03909 509.7798
chisq.test(voter$PartyRegistration,voter$immi_china)[6]
## $observed
## voter$immi_china
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 254 120 678
## Republican 422 37 433
table(voter$PartyRegistration,voter$immi_africa)%>%
prop.table(1)
##
## Decrease Increase Keep about the same
## Democrat 0.20892688 0.15479582 0.63627730
## Republican 0.51293588 0.03937008 0.44769404
chisq.test(voter$PartyRegistration,voter$immi_africa)
##
## Pearson's Chi-squared test
##
## data: voter$PartyRegistration and voter$immi_africa
## X-squared = 222.15, df = 2, p-value < 2.2e-16
chisq.test(voter$PartyRegistration,voter$immi_africa)[7]
## $expected
## voter$immi_africa
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 366.5438 107.36045 579.0958
## Republican 309.4562 90.63955 488.9042
chisq.test(voter$PartyRegistration,voter$immi_africa)[6]
## $observed
## voter$immi_africa
## voter$PartyRegistration Decrease Increase Keep about the same
## Democrat 220 163 670
## Republican 456 35 398
dem_voter<-voter%>%
filter(PartyRegistration=="Democrat")
Dem_Samp_Distro<-
replicate(10000,
sample(dem_voter$ft_immig_2017, 60)%>%
mean(na.rm=TRUE))%>%
data.frame()%>%
rename("mean"=1)
ggplot()+
geom_histogram(data=Dem_Samp_Distro, aes(x=mean), fill="blue")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
rep_voter<-voter%>%
filter(PartyRegistration=="Republican")
Rep_Samp_Distro<-
replicate(10000,
sample(rep_voter$ft_immig_2017, 60)%>%
mean(na.rm=TRUE))%>%
data.frame()%>%
rename("mean"=1)
ggplot()+
geom_histogram(data=Rep_Samp_Distro, aes(x=mean), fill="red")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
t.test(ft_immig_2017~PartyRegistration, data=voter)
##
## Welch Two Sample t-test
##
## data: ft_immig_2017 by PartyRegistration
## t = 15.587, df = 2285.1, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 14.50152 18.67546
## sample estimates:
## mean in group Democrat mean in group Republican
## 69.75725 53.16876