setwd("~/Downloads/R /Homeowrk_6_Alessia_DeFoa") # Replace /path/to/your/folder with the actual path you just copied.
getwd()
## [1] "/Users/alessiamacbook/Downloads/R /Homeowrk_6_Alessia_DeFoa"
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
# 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.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ 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: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
## [[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] "kableExtra" "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] "rmarkdown" "kableExtra" "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" "kableExtra" "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"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "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] "questionr" "ggridges" "rmarkdown" "kableExtra" "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"
#install.packages("ggplot2")
packages <- c("ggplot2")
#install.packages("tidyverse")
packages <- ("tidyverse")
library(ggplot2)
library(tidyverse)
ess <- read_fst("All-ESS-Data.fst")
Mission 2
sweden_data <- ess %>%
filter(cntry == "SE")
Mission 3
write_fst(sweden_data, "~/Downloads/R /Homeowrk_6_Alessia_DeFoa/sweden_data.fst")
rm(list=ls()); gc()
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 1269837 67.9 2115632 113.0 NA 2115632 113.0
## Vcells 2172769 16.6 1257054204 9590.6 16384 1353865583 10329.2
df <- read_fst("~/Downloads/R /Homeowrk_6_Alessia_DeFoa/sweden_data.fst")
Mission 4
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]
}
sweden_data <- df
sweden_data_table_subset <- sweden_data %>%
mutate(
pray = ifelse(pray == 2, 0, ifelse(pray %in% c(77, 88, 99), NA, pray)),
rlgdnm = ifelse(rlgdnm %in% c(77, 88, 99), NA, rlgdnm),
rlgdnme = ifelse(rlgdnme %in% c(7, 8, 9), NA, rlgdnme)
)
summary_table <- datasummary_skim(sweden_data_table_subset %>% select(pray, rlgdnm, rlgdnme), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
pray | 8 | 1 | 5.7 | 2.1 | 0.0 | 7.0 | 7.0 |
rlgdnm | 10 | 0 | 45.2 | 29.9 | 1.0 | 66.0 | 66.0 |
rlgdnme | 10 | 0 | 60.2 | 18.4 | 1.0 | 66.0 | 99.0 |
flextable::save_as_docx(summary_table, path = "table 1- descriptive statistics.docx",
width = 7.0, height = 7.0)
Mission 5
sweden_data_v2 <- sweden_data_table_subset %>%
rename(
`How often pray apart from at religious services` = pray,
`Religion or denomonation belonging at present` = rlgdnm,
`Religion or denomonation belonging to in the past` = rlgdnme
)
summary_table_v2 <- datasummary_skim(sweden_data_v2 %>% select(`How often pray apart from at religious services`,`Religion or denomonation belonging at present`,`Religion or denomonation belonging to in the past` ), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table_v2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
How often pray apart from at religious services | 8 | 1 | 5.7 | 2.1 | 0.0 | 7.0 | 7.0 |
Religion or denomonation belonging at present | 10 | 0 | 45.2 | 29.9 | 1.0 | 66.0 | 66.0 |
Religion or denomonation belonging to in the past | 10 | 0 | 60.2 | 18.4 | 1.0 | 66.0 | 99.0 |
Mission 6
avg_pray_by_year <- aggregate(pray ~ year + happy, data=sweden_data_table_subset, mean)
# Storing plot as p1
p1 <- ggplot(avg_pray_by_year, aes(x=year, y=pray, color=as.factor(happy))) +
geom_line(aes(group=happy)) +
labs(title="Mean of how often one prays apart from a religious service by year",
subtitle = "for those that are happy vs not in Sweden",
x="Survey year",
y="Average time you pray apart from a religious service") +
scale_color_discrete(name="those that are happy ", labels=c("No", "Yes")) +
theme_minimal()
p1
sweden_data_table_subset$yrbrn[sweden_data_table_subset$yrbrn %in% c(7777, 8888)] <- NA
avg_pray_by_yrbrn <- aggregate(pray ~ yrbrn, data=subset(sweden_data_table_subset, happy == 0 & yrbrn >= 1985 & yrbrn <= 2005), FUN=mean)
# Plot using ggplot
p2 <- ggplot(avg_pray_by_yrbrn, aes(x=yrbrn, y=pray)) +
geom_point(aes(), size=3) +
geom_smooth(aes(), se=FALSE) +
labs(title="Mean of how often one prays apart from a religious service by year of birth",
subtitle = "for those that are happy vs not in Sweden",
x="Birth year",
y="Average",
color="Legend") +
theme_minimal()
p2
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : span too small. fewer data values than degrees of freedom.
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : pseudoinverse used at 1986
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : neighborhood radius 5.035
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : There are other near singularities as well. 4.1412
ggsave(filename = "plot1.pdf", plot = p1, device = "pdf", width = 6, height = 4)
ggsave(filename = "plot2.pdf", plot = p2, device = "pdf", width = 6, height = 4)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : span too small. fewer data values than degrees of freedom.
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : pseudoinverse used at 1986
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : neighborhood radius 5.035
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : reciprocal condition number 0
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
## : There are other near singularities as well. 4.1412