| Kontak | \(\downarrow\) |
| naftaligunawan@gmail.com | |
| https://www.instagram.com/nbrigittag/ | |
| RPubs | https://rpubs.com/naftalibrigitta/ |
| Nama | Naftali Brigitta Gunawan |
| NIM | 20214920002 |
GDP & Unemployment Rate
Question & Import Data
Let’s consider a scenario where we want to analyze the relationship between a country’s GDP (Gross Domestic Product) and its unemployment rate. The hypothesis is that higher GDP leads to lower unemployment rates due to increased economic activity and job creation. First, we’ll generate a simulated dataset with two variables: GDP and Unemployment Rate. We’ll assume a linear relationship between the two variables with some random noise.
set.seed(123) # For reproducibility
# Generate simulated data
n <- 10000 # Number of observations
gdp <- rnorm(n, mean = 1000, sd = 200) # Simulated GDP data
unemployment <- 10 - 0.05 * gdp + rnorm(n, mean = 0, sd = 2) # Simulated unemployment data
# Create a data frame
tada <- data.frame(GDP = gdp, Unemployment = unemployment)
head(tada)
Job 1: Explore the data visually to understand the relationship between GDP and unemployment rate
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.3
##
## 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
library(ggplot2)
gg <- ggplot(tada, aes(x = gdp, y = unemployment)) +
geom_point(color = "yellow") +
geom_smooth(method = "lm", se = FALSE, color = "blue") +
labs(title = "Relationship Between GDP and Unemployment Rate", x = "GDP", y = "Unemployment") +
theme_minimal()
ggplotly(gg)
## `geom_smooth()` using formula = 'y ~ x'
Note: The graphic describe, if the GDP is high >>> the unemployment is low, and if the unemployment is high >> the GDP is low.
Job 2: Perform simple linear regression to quantify the relationship between GDP and unemployment rate
linearjob2 <- lm(formula = unemployment ~ gdp, data = tada)
summary(linearjob2)
##
## Call:
## lm(formula = unemployment ~ gdp, data = tada)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.9652 -1.3378 -0.0148 1.3617 7.5393
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.9214218 0.1022358 97.04 <2e-16 ***
## gdp -0.0499396 0.0001003 -497.89 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.003 on 9998 degrees of freedom
## Multiple R-squared: 0.9612, Adjusted R-squared: 0.9612
## F-statistic: 2.479e+05 on 1 and 9998 DF, p-value: < 2.2e-16
Job 3: Interpret the relationship between GDP and unemployment rate.
The output of simple linear regression is Y = 9.93 - 0.05X or Unemployment = 9.93 - 0.05 GDP. It’s make a meaning, if GDP is 0, so the employment is 9.93, and if the GDP has increased, so the GDP decreased 0.05 unemployment.
GDP Growth Rate and Invesment Rate
Question & Import Data
The objective of this study case is to demonstrate how simple linear regression can be used to analyze economic data and make predictions based on the relationship between two variables. Lets generate data for GDP growth rate (gdp_growth) and investment rate (investment_rate) for a fictional country over a period of 10 years.
# Set seed for reproducibility
set.seed(123)
# Generate data
years <- 1:10
investment_rate <- rnorm(10, mean = 20, sd = 5)
gdp_growth <- 3 + 0.8 * investment_rate + rnorm(10, mean = 0, sd = 1)
# Create a data frame
grow <- data.frame(years, investment_rate, gdp_growth)
grow
Job 1: Perform simple linear regression analysis to understand the relationship between GDP growth rate and investment rate
library(plotly)
library(ggplot2)
gg <- ggplot(grow, aes(x = investment_rate, y = gdp_growth)) +
geom_point(color = "red") +
geom_smooth(method = "lm", se = FALSE, color = "black") +
labs(title = "Relationship Between GDP and Unemployment Rate", x = "GDP", y = "Unemployment") +
theme_minimal()
ggplotly(gg)
## `geom_smooth()` using formula = 'y ~ x'
linearjob1 <- lm(formula = gdp_growth ~ investment_rate, data = grow)
summary(linearjob1)
##
## Call:
## lm(formula = gdp_growth ~ investment_rate, data = grow)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.33303 -0.64421 -0.02448 0.49596 1.41472
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.64706 1.31108 0.494 0.635
## investment_rate 0.92573 0.06282 14.736 4.42e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8988 on 8 degrees of freedom
## Multiple R-squared: 0.9645, Adjusted R-squared: 0.96
## F-statistic: 217.1 on 1 and 8 DF, p-value: 4.423e-07
Job 2: Make predictions about future GDP growth rates based on different levels of investment
predictions <- data.frame(investment_rate = c(40, 45, 50))
gdppredictions <- predict(linearjob1, newdata = predictions)
data_predictions <- data.frame(investment_rate = predictions$investment_rate, gdppredictions)
data_predictions
Note: The creator make a investment rate prediction start from 40, 45, and 50.
Job 3: Gained insights into how changes in investment may influence economic growth.
If the investment_rate is 40, the gdp_predictions is 37.67635 and still grow along with investment rate.