packages <- c("tidyverse", "fst", "modelsummary", "broom", "sjPlot", "ggplot2", "car", "Lock5Data", "mosaic")
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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
## `modelsummary` 2.0.0 now uses `tinytable` as its default table-drawing
##   backend. Learn more at: https://vincentarelbundock.github.io/tinytable/
## 
## Revert to `kableExtra` for one session:
## 
##   options(modelsummary_factory_default = 'kableExtra')
##   options(modelsummary_factory_latex = 'kableExtra')
##   options(modelsummary_factory_html = 'kableExtra')
## 
## Silence this message forever:
## 
##   config_modelsummary(startup_message = FALSE)
## 
## Install package "strengejacke" from GitHub (`devtools::install_github("strengejacke/strengejacke")`) to load all sj-packages at once!
## 
## Loading required package: carData
## 
## 
## Attaching package: 'car'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## 
## The following object is masked from 'package:purrr':
## 
##     some
## 
## 
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## 
## Attaching package: 'mosaic'
## 
## 
## The following object is masked from 'package:Matrix':
## 
##     mean
## 
## 
## The following objects are masked from 'package:car':
## 
##     deltaMethod, logit
## 
## 
## The following object is masked from 'package:modelsummary':
## 
##     msummary
## 
## 
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## 
## 
## The following object is masked from 'package:purrr':
## 
##     cross
## 
## 
## The following object is masked from 'package:ggplot2':
## 
##     stat
## 
## 
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## 
## 
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "fst"       "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "modelsummary" "fst"          "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "broom"        "modelsummary" "fst"          "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "sjPlot"       "broom"        "modelsummary" "fst"          "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[6]]
##  [1] "sjPlot"       "broom"        "modelsummary" "fst"          "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[7]]
##  [1] "car"          "carData"      "sjPlot"       "broom"        "modelsummary"
##  [6] "fst"          "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "Lock5Data"    "car"          "carData"      "sjPlot"       "broom"       
##  [6] "modelsummary" "fst"          "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "mosaic"       "mosaicData"   "ggformula"    "Matrix"       "lattice"     
##  [6] "Lock5Data"    "car"          "carData"      "sjPlot"       "broom"       
## [11] "modelsummary" "fst"          "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"
library(readr)
library(ggplot2)
towny = read_csv("C:/Users/tophe/Downloads/towny.csv")
## Rows: 414 Columns: 26
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (5): name, website, status, csd_type, census_div
## dbl (21): rownames, latitude, longitude, land_area_km2, population_1996, pop...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
towny_filtered <- towny %>%
  dplyr::select(pop_change_2016_2021_pct, pop_change_2011_2016_pct, pop_change_2006_2011_pct, population_2021, latitude, csd_type)
datasummary_skim(towny_filtered)
tinytable_jo9b4uq91r23ozo6pvqu
Unique Missing Pct. Mean SD Min Median Max Histogram
pop_change_2016_2021_pct 380 0 0.1 0.1 -0.2 0.0 0.8
pop_change_2011_2016_pct 363 0 0.0 0.1 -0.4 0.0 0.5
pop_change_2006_2011_pct 383 0 0.0 0.1 -0.4 0.0 0.6
population_2021 406 0 34141.7 160813.8 16.0 6489.0 2794356.0
latitude 316 0 45.3 2.0 41.8 44.9 51.5
csd_type N %
city 52 12.6
municipality 68 16.4
town 88 21.3
township 195 47.1
village 11 2.7
ggplot(towny, aes(x = reorder(csd_type, -pop_change_2016_2021_pct, FUN = median), y = pop_change_2016_2021_pct, fill = csd_type)) +
  geom_boxplot() +
  theme_minimal() +
  theme(legend.position = "right") +
  labs(
    title = "Boxplots for Proportional Change in Population by Subdivision",
    x = "Subdivision", 
    y = "Proportional Change in Population" )
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_boxplot()`).

Model1 <- lm(pop_change_2016_2021_pct ~ pop_change_2011_2016_pct + pop_change_2006_2011_pct + pop_change_2001_2006_pct, data = towny)
Model2 <- lm(pop_change_2016_2021_pct ~ latitude, data = towny)
Model3 <- lm(pop_change_2016_2021_pct ~ population_2021 + csd_type, data = towny)
models_world <- list(Model1, Model2, Model3)
tab_model(models_world)
  pop_change_2016_2021_pct pop_change_2016_2021_pct pop_change_2016_2021_pct
Predictors Estimates CI p Estimates CI p Estimates CI p
(Intercept) 0.05 0.04 – 0.06 <0.001 0.65 0.45 – 0.85 <0.001 0.06 0.03 – 0.08 <0.001
pop change 2011 2016 pct 0.06 -0.05 – 0.16 0.296
pop change 2006 2011 pct 0.14 0.04 – 0.23 0.007
pop change 2001 2006 pct 0.31 0.22 – 0.39 <0.001
latitude -0.01 -0.02 – -0.01 <0.001
population 2021 0.00 -0.00 – 0.00 0.869
csd type [municipality] 0.00 -0.03 – 0.04 0.877
csd type [town] 0.01 -0.03 – 0.04 0.689
csd type [township] 0.00 -0.03 – 0.03 0.994
csd type [village] -0.05 -0.12 – 0.01 0.090
Observations 410 413 413
R2 / R2 adjusted 0.151 / 0.145 0.076 / 0.073 0.010 / -0.002
ggplot(towny, aes(x = population_2021, y = pop_change_2016_2021_pct, color = csd_type)) +
  geom_point(outlier.shape = NA, size = 0.5) +
  labs(x = "Population in 2021", y = "Proportional change in Population", title = "Proportional Change in Population by Population Size, 2016-2021")
## Warning in geom_point(outlier.shape = NA, size = 0.5): Ignoring unknown
## parameters: `outlier.shape`
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).