# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer")
# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: viridisLite
##
##
## Attaching package: 'flextable'
##
##
## The following object is masked from 'package:purrr':
##
## compose
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[8]]
## [1] "rmarkdown" "knitr" "viridis" "viridisLite" "fst"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "ggridges" "rmarkdown" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[11]]
## [1] "questionr" "ggridges" "rmarkdown" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[12]]
## [1] "flextable" "questionr" "ggridges" "rmarkdown" "knitr"
## [6] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[13]]
## [1] "infer" "flextable" "questionr" "ggridges" "rmarkdown"
## [6] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [11] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
# read data
ess <- read_fst("All-ESS-Data.fst")
france_data <- ess %>%
filter(cntry == "FR")
write_fst(france_data, "~/Documents/Things/Study&Work/UofT/Study/Y2 Fall/SOC202 Quantitative/R/WorkingFile/final_project.fst")
df <- read_fst("~/Documents/Things/Study&Work/UofT/Study/Y2 Fall/SOC202 Quantitative/R/WorkingFile/final_project.fst")
##unique(france_data$hinctnt) ## [1] NA 6 77 9 4 2 88 7 5 8 11 3 12 10 1
##unique(france_data$health) ##[1] 2 1 3 5 4 8 7
##unique(france_data$happy) ## [1] 10 3 6 5 8 7 9 4 2 0 1 88 77
df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
df$year[df$essround == i] <- replacements[i]
}
france_data <- df
#
fdts <- france_data %>%
mutate(
hinctnt = ifelse(hinctnt %in% c(77, 88,NA), 1,
hinctnt),
happy = ifelse(happy %in% c(77, 88, 99), 0, happy)
)
frtmp <- france_data %>%
mutate(
hinctnt = ifelse(hinctnt %in% c(77, 88,NA), 1, hinctnt),
happy = ifelse(happy %in% c(77, 88, 99), 0, happy)
)
fdts$hinctnt <- ifelse(fdts$hinctnt >= 7,1,0)
france_data_v2 <- fdts %>%
rename(
`Household's total net income, all sources` = hinctnt,
`Subjective General Health` = health,
`How happy are you - important aspect of mental well being` = happy
)
summary_table <- datasummary_skim(france_data_v2 %>% select(`Household's total net income, all sources`, `Subjective General Health`, `How happy are you - important aspect of mental well being`),title="Table 1:Descriptive Statistics of Variables")#, output = "flextable")
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| Household’s total net income, all sources | 2 | 0 | 0.1 | 0.3 | 0.0 | 0.0 | 1.0 | |
| Subjective General Health | 7 | 0 | 2.3 | 0.9 | 1.0 | 2.0 | 8.0 | |
| How happy are you - important aspect of mental well being | 11 | 0 | 7.2 | 1.8 | 0.0 | 8.0 | 10.0 |
fdts$hinctnt<-as.character(fdts$hinctnt)
fdts$happy<-as.character(fdts$happy)
fdts$health<-as.character(fdts$health)
test_stat <- fdts %>%
specify(explanatory = hinctnt,
response = happy) %>%
hypothesize(null = "independence") %>%
calculate(stat = "Chisq") # Applying Chi-squred test
print(test_stat$stat)
## X-squared
## 98.4129
null_distribution <- fdts %>%
specify(explanatory = hinctnt, response = happy) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "Chisq")
p_val <- null_distribution %>% # replace name here if you assigned something other than null_distribution above
get_pvalue(obs_stat = test_stat, direction = "two-sided") # would only replace test_stat if assigned another name in Step 1
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: happy (factor)
## Explanatory: hinctnt (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 13.7
## 2 2 17.0
## 3 3 5.76
## 4 4 19.3
## 5 5 15.9
## 6 6 10.5
## 7 7 10.7
## 8 8 10.8
## 9 9 10.2
## 10 10 13.2
## # ℹ 990 more rows
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
null_distribution
## Response: happy (factor)
## Explanatory: hinctnt (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 13.7
## 2 2 17.0
## 3 3 5.76
## 4 4 19.3
## 5 5 15.9
## 6 6 10.5
## 7 7 10.7
## 8 8 10.8
## 9 9 10.2
## 10 10 13.2
## # ℹ 990 more rows
frtmp$hinctnt <- ifelse(frtmp$hinctnt >= 7,1,0)
frtmp$hinctnt<-as.character(frtmp$hinctnt)
ggplot(frtmp, aes(hinctnt, happy)) +
geom_boxplot(outlier.shape = NA,)+ ggtitle("Figure 2.Boxplot Comparision for Income and Happy ")+theme(plot.title = element_text( size = 10, hjust = 0.5))# not showing abnormal value
# regression
model1<-lm(happy~ hinctnt,data = fdts)
model2 <-lm(happy ~ hinctnt+ health, data = fdts)
model3 <-lm(health ~ hinctnt,data = fdts)
modelsummary::modelsummary(title="Table 2.Regression Modeling Object",
list(model1,model2,model3),
gof_map = c("nobs","r.squared","adj.r.squared","rmse"))
| (1) | (2) | (3) | |
|---|---|---|---|
| (Intercept) | 7.157 | 7.890 | 2.308 |
| (0.014) | (0.029) | (0.007) | |
| hinctnt1 | 0.403 | 0.287 | −0.194 |
| (0.047) | (0.045) | (0.023) | |
| health2 | −0.566 | ||
| (0.035) | |||
| health3 | −1.043 | ||
| (0.037) | |||
| health4 | −1.879 | ||
| (0.056) | |||
| health5 | −2.739 | ||
| (0.115) | |||
| health7 | −3.890 | ||
| (0.655) | |||
| health8 | −1.140 | ||
| (0.867) | |||
| Num.Obs. | 19038 | 19038 | 19038 |
| R2 | 0.004 | 0.091 | 0.004 |
| R2 Adj. | 0.004 | 0.090 | 0.004 |
| RMSE | 1.81 | 1.73 | 0.90 |