This dataset contains statistics regarding the population of all the countries and cities that are in the UN. The source for my data comes from a yearly consensus made by the UN that is publically available to view. The variables in the dataset contain the country, the area, gender, city, population, and I will be exploring the variables mentioned.
The purpose of this project is to discover if there is a relationship between the variales and population. A relationship regarding if the countries size or gender level affects the population in any way, focusing on Baltimore in the United states.
Load the libraries
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.5.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── 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
library(ggplot2)library(readr)library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
Rows: 31025 Columns: 11
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): Country or Area, Year, Area, Sex, City, City type, Record Type, Rel...
dbl (2): Source Year, Value
num (1): Value Footnotes
ℹ 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.
head(population_data)
# A tibble: 6 × 11
`Country or Area` Year Area Sex City `City type` `Record Type`
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Åland Islands 2024 Total Both Sexes MARIEHAMN City proper Estimate - de …
2 Åland Islands 2023 Total Both Sexes MARIEHAMN City proper Estimate - de …
3 Åland Islands 2022 Total Both Sexes MARIEHAMN City proper Estimate - de …
4 Åland Islands 2021 Total Both Sexes MARIEHAMN City proper Census - de ju…
5 Åland Islands 2021 Total Both Sexes MARIEHAMN City proper Estimate - de …
6 Åland Islands 2020 Total Both Sexes MARIEHAMN City proper Estimate - de …
# ℹ 4 more variables: Reliability <chr>, `Source Year` <dbl>, Value <dbl>,
# `Value Footnotes` <dbl>
Narrow down all the values to focus on baltimore and remove duplicates
baltimore_population <- population_data |>filter( country_or_area =="United States of America", city =="Baltimore (MD)", area =="Total", sex =="Both Sexes" ) |>mutate(year =as.numeric(year),value =as.numeric(value) ) |>filter(!is.na(year), !is.na(value)) |>distinct(year, .keep_all =TRUE) |>arrange(year)head(baltimore_population)
# A tibble: 6 × 11
country_or_area year area sex city city_type record_type reliability
<chr> <dbl> <chr> <chr> <chr> <chr> <chr> <chr>
1 United States of Am… 2000 Total Both… Balt… City pro… Census - d… Final figu…
2 United States of Am… 2005 Total Both… Balt… City pro… Estimate -… Final figu…
3 United States of Am… 2007 Total Both… Balt… City pro… Estimate -… Final figu…
4 United States of Am… 2008 Total Both… Balt… City pro… Estimate -… Final figu…
5 United States of Am… 2009 Total Both… Balt… City pro… Estimate -… Final figu…
6 United States of Am… 2010 Total Both… Balt… City pro… Census - d… Final figu…
# ℹ 3 more variables: source_year <dbl>, value <dbl>, value_footnotes <dbl>
Visualize the data
ggplot( baltimore_population,aes(x = year, y = value)) +geom_point() +geom_smooth(method ="lm", se =TRUE) +labs(title ="Baltimore Population Over the years",x ="Year",y ="Population" ) +theme_classic()
`geom_smooth()` using formula = 'y ~ x'
Linear Regression
population_model <-lm( value ~ year,data = baltimore_population)summary(population_model)
Call:
lm(formula = value ~ year, data = baltimore_population)
Residuals:
Min 1Q Median 3Q Max
-11774.0 -7972.8 -659.2 4615.3 16341.6
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8319009.5 758978.2 10.96 2.93e-07 ***
year -3828.0 377.1 -10.15 6.35e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 9441 on 11 degrees of freedom
Multiple R-squared: 0.9036, Adjusted R-squared: 0.8948
F-statistic: 103.1 on 1 and 11 DF, p-value: 6.353e-07
What the linear regression means
Based on our linear model, you can that is used to estimate the relationship between year and population, it is apparent that the linear relationship is statistically significant, the r squared value helps us determine the variation in population. More than 90% of the population’s variation can be explained by the linear model (year), an downward relationship with the year
Conclusion
Overall, this project was tricky as I had to go back and nitpick little pieces of code posted in the notes as well as looking at exmaples of project 1. My favorite part was seeing everything coming together for the visualization. The parts where I struggled were getting all the code to work and filtering/cleaning out the columns and rows I intended on using. The baltimore population was broken down and explained in linear model and through visualization.