# Load necessary libraries

library(pacman)
# Load required packages
p_load(ggplot2, car, outliers, dplyr, gridExtra, stargazer, patchwork, lmtest, broom, cowplot, tidyr, kableExtra, plm)
# Data Description -----------------------------------------------------------


# Load data
data <- read.csv("/Users/leonhardromer/Desktop/PPE/Fourth Semester/Quantitative/Quantitative Essay/GDPfromOuterSpaceContinent.csv")

# Clean Continent variable
clean_continent <- function(data) {
  data$Continent <- trimws(data$Continent)  # Remove leading/trailing spaces
  data$Continent <- tolower(data$Continent)  # Convert to lowercase
  data$Continent[data$Continent == "oceania"] <- "Oceania"  # Standardize "Oceania"
  return(data)
}
data <- clean_continent(data)

# Add Year variable and convert country to factor
data$Year <- rep(c(1992, 2005), each = nrow(data)/2)
data$country <- as.factor(data$country)

# Subset relevant variables
relevant_data <- data[c("light", "gdp", "prior_light", "prior_gdp", "Continent", "Year")]



# Apply logarithmic transformation and standardization
relevant_data <- relevant_data %>%
  mutate(
    log_gdp = log(gdp),
    log_prior_gdp = log(prior_gdp),
    log_light = log(light),
    log_prior_light = log(prior_light),
    log_gdp_standardized = scale(log_gdp),
    log_prior_gdp_standardized = scale(log_prior_gdp),
    log_light_standardized = scale(log_light),
    log_prior_light_standardized = scale(log_prior_light)
  )


# Inspect the structure of relevant_data dataframe
str(relevant_data)
## 'data.frame':    170 obs. of  14 variables:
##  $ light                       : num  4.23 11.93 10.57 1.45 11.86 ...
##  $ gdp                         : num  11.11 543.02 2.15 16.56 1.1 ...
##  $ prior_light                 : num  4.482 11.808 9.699 0.735 13.392 ...
##  $ prior_gdp                   : num  7.373 464.168 1.793 4.901 0.754 ...
##  $ Continent                   : chr  "america" "asia" "europe" "asia" ...
##  $ Year                        : num  1992 1992 1992 1992 1992 ...
##  $ log_gdp                     : num  2.4076 6.2971 0.7665 2.8069 0.0938 ...
##  $ log_prior_gdp               : num  1.998 6.14 0.584 1.589 -0.283 ...
##  $ log_light                   : num  1.441 2.479 2.358 0.372 2.473 ...
##  $ log_prior_light             : num  1.5 2.469 2.272 -0.308 2.595 ...
##  $ log_gdp_standardized        : num [1:170, 1] 1.079 2.03 0.678 1.177 0.513 ...
##   ..- attr(*, "scaled:center")= num -2
##   ..- attr(*, "scaled:scale")= num 4.09
##  $ log_prior_gdp_standardized  : num [1:170, 1] 1.105 2.13 0.754 1.003 0.54 ...
##   ..- attr(*, "scaled:center")= num -2.46
##   ..- attr(*, "scaled:scale")= num 4.04
##  $ log_light_standardized      : num [1:170, 1] 0.78 1.307 1.245 0.237 1.304 ...
##   ..- attr(*, "scaled:center")= num -0.0942
##   ..- attr(*, "scaled:scale")= num 1.97
##  $ log_prior_light_standardized: num [1:170, 1] 0.939 1.41 1.3144 0.0599 1.4712 ...
##   ..- attr(*, "scaled:center")= num -0.431
##   ..- attr(*, "scaled:scale")= num 2.06
# Variable Descriptions -------------------------------------------------------

# Variable descriptions
# The variable 'light' represents light emissions.
# The variable 'gdp' represents Gross Domestic Product.
# The variable 'prior_light' represents prior light emissions.
# The variable 'prior_gdp' represents prior Gross Domestic Product.
# The variable 'Continent' represents the continent to which the country belongs.
# The variable 'Year' represents the year of observation.
# Calculate descriptive statistics
descriptive_stats <- data.frame(
  variable = c("Log_Light", "Log_Prior_Light", "Log_GDP", "Log_Prior_GDP"),
  mean = round(c(mean(relevant_data$log_light_standardized), mean(relevant_data$log_prior_light_standardized), 
                 mean(relevant_data$log_gdp_standardized), mean(relevant_data$log_prior_gdp_standardized)), 2),
  median = round(c(median(relevant_data$log_light_standardized), median(relevant_data$log_prior_light_standardized), 
                   median(relevant_data$log_gdp_standardized), median(relevant_data$log_prior_gdp_standardized)), 2),
  sd = round(c(sd(relevant_data$log_light_standardized), sd(relevant_data$log_prior_light_standardized), 
               sd(relevant_data$log_gdp_standardized), sd(relevant_data$log_prior_gdp_standardized)), 2)
)


# Print the descriptive statistics
print(descriptive_stats)
##          variable mean median sd
## 1       Log_Light    0   0.16  1
## 2 Log_Prior_Light    0   0.16  1
## 3         Log_GDP    0   0.16  1
## 4   Log_Prior_GDP    0   0.13  1
# Save the table to a file
save_kable(descriptive_stats, file = "descriptive_stats_table.html")
# Plot Presentation
font_settings <- theme_minimal() + 
  theme(axis.text = element_text(size = 14),
        axis.title = element_text(size = 16, face = "bold"),
        plot.title = element_text(size = 20, face = "bold"))

# Histograms of standardized light emissions by continent
histogram_light_before <- ggplot(relevant_data, aes(x = log_prior_light_standardized, fill = Continent)) + 
  geom_histogram(binwidth = 0.5, color = "black", alpha = 0.5) + 
  labs(x = "Standardized Prior Light Emissions", y = "Density", 
       title = "Distribution of Standardized Prior Light Emissions") + 
  facet_wrap(~ Continent, ncol=3) +
  font_settings +
  # Interpretation
  labs(caption = "The histograms display the standardized distribution of Night-time light emissions,
       1992-1993 across different continents.")

histogram_light_after <- ggplot(relevant_data, aes(x = log_light_standardized, fill = Continent)) + 
  geom_histogram(binwidth = 0.5, color = "black", alpha = 0.5) + 
  labs(x = "Standardized Light Emissions", y = "Density", 
       title = "Distribution of Standardized Light Emissions") + 
  facet_wrap(~ Continent, ncol=3) +
  font_settings +
  # Interpretation
  labs(caption = "The histograms display the standardized distribution of Night-time light emissions,
       2005-2006 across different continents.")

# Histograms of standardized GDP by continent
histogram_gdp_before <- ggplot(relevant_data, aes(x = log_prior_gdp_standardized, fill = Continent)) + 
  geom_histogram(binwidth = 0.5, color = "black", alpha = 0.5) + 
  labs(x = "Standardized Prior GDP", y = "Density", 
       title = "Distribution of Standardized Prior GDP") + 
  facet_wrap(~Continent, ncol = 2) +
  font_settings +
  # Interpretation
  labs(caption = "The histograms display the standardized distribution of Economic output of a country,
       1992-1993 across different continents.")

histogram_gdp_after <- ggplot(relevant_data, aes(x = log_gdp_standardized, fill = Continent)) + 
  geom_histogram(binwidth = 0.5, color = "black", alpha = 0.5) + 
  labs(x = "Standardized GDP", y = "Density", 
       title = "Distribution of Standardized GDP") + 
  facet_wrap(~Continent, ncol = 2) +
  font_settings +
  # Interpretation
  labs(caption = "The histograms display the standardized distribution of Economic output of a country,
       2005-2006 across different continents.")

# Create a scatter plot of standardized light emissions and economic growth
scatterplot <- ggplot(relevant_data, aes(x = log_light_standardized, y = log_gdp_standardized)) + 
  geom_point() + 
  geom_smooth(method = "lm", se = TRUE, color = "blue") + 
  labs(title = "Relationship between Standardized Light Emissions and Standardized GDP", 
       x = "Standardized Light Emissions", y = "Standardized GDP") + 
  font_settings

# Box plots of standardized light emissions and GDP by continent

box_plot_light <- ggplot(relevant_data, aes(x = Continent, y = log_light_standardized)) +
  geom_boxplot(fill = "blue", alpha = 0.5) +
  labs(x = "Continent", y = "Standardized Light Emissions",
       title = "Distribution of Standardized Light Emissions by Continent") +
  font_settings +
  # Interpretation
  labs(caption = "The box plots visualize the distribution of standardized light emissions across
       different continents.")

box_plot_gdp <- ggplot(relevant_data, aes(x = Continent, y = log_gdp_standardized)) +
  geom_boxplot(fill = "blue", alpha = 0.5) +
  labs(x = "Continent", y = "Standardized GDP",
       title = "Distribution of Standardized GDP by Continent") +
  font_settings +
  # Interpretation
  labs(caption = "The box plots display the distribution of standardized GDP across
       different continents.")

# Line plot of GDP over Time
lineplot_gdp <- ggplot(relevant_data, aes(x = Year, y = log_gdp_standardized)) + 
  geom_line(color = "blue") + 
  labs(x = "Year", y = "Standardized GDP", title = "Standardized GDP over Time") + 
  font_settings

# Print the visualizations
print(scatterplot)
## `geom_smooth()` using formula = 'y ~ x'

print(box_plot_light)

print(box_plot_gdp)

print(lineplot_gdp)

print(histogram_light_before)

print(histogram_light_after)

print(histogram_gdp_before)

print(histogram_gdp_after)

# Linear Regression ----------------------------------------------------------
# Define the linear regression model
model <- lm(log_gdp_standardized ~ log_light_standardized, data = relevant_data)

# Extract the coefficient for light emissions from the model
light_coefficient <- coef(model)["log_light_standardized"]

# Calculate the elasticity
elasticity <- light_coefficient * mean(relevant_data$log_light) / mean(relevant_data$log_gdp)

# Print the elasticity
cat("Elasticity of GDP with respect to light emissions:", elasticity, "\n")
## Elasticity of GDP with respect to light emissions: 0.004363286
# Model interpretation
stargazer_output <- stargazer(model, type = "text", title = "Linear Regression Results", 
                              caption = "Results of the linear regression model predicting standardized GDP from standardized light emissions, prior standardized light emissions, and prior standardized GDP.")
## 
## Linear Regression Results
## ==================================================
##                            Dependent variable:    
##                        ---------------------------
##                           log_gdp_standardized    
## --------------------------------------------------
## log_light_standardized            0.093           
##                                  (0.077)          
##                                                   
## Constant                          0.000           
##                                  (0.077)          
##                                                   
## --------------------------------------------------
## Observations                       170            
## R2                                0.009           
## Adjusted R2                       0.003           
## Residual Std. Error         0.999 (df = 168)      
## F Statistic                1.460 (df = 1; 168)    
## ==================================================
## Note:                  *p<0.1; **p<0.05; ***p<0.01
## 
## Linear Regression Results
## =====================================================================================================================================================================
## Results of the linear regression model predicting standardized GDP from standardized light emissions, prior standardized light emissions, and prior standardized GDP.
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
cat(stargazer_output)
##  Linear Regression Results ==================================================                            Dependent variable:                            ---------------------------                           log_gdp_standardized     -------------------------------------------------- log_light_standardized            0.093                                             (0.077)                                                              Constant                          0.000                                             (0.077)                                                              -------------------------------------------------- Observations                       170             R2                                0.009            Adjusted R2                       0.003            Residual Std. Error         0.999 (df = 168)       F Statistic                1.460 (df = 1; 168)     ================================================== Note:                  *p<0.1; **p<0.05; ***p<0.01  Linear Regression Results ===================================================================================================================================================================== Results of the linear regression model predicting standardized GDP from standardized light emissions, prior standardized light emissions, and prior standardized GDP. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Extract the coefficients and their standard errors from the model
coefficients_df <- tidy(model)


if (!requireNamespace("knitr", quietly = TRUE)) {
  install.packages("knitr")
}
library(knitr)

# Create a table with the coefficients and their standard errors
coefficients_table <- knitr::kable(coefficients_df, caption = "Coefficients and Standard Errors of the Linear Regression Model")

print(coefficients_table)
## 
## 
## Table: Coefficients and Standard Errors of the Linear Regression Model
## 
## |term                   | estimate| std.error| statistic|   p.value|
## |:----------------------|--------:|---------:|---------:|---------:|
## |(Intercept)            | 0.000000| 0.0765924|  0.000000| 1.0000000|
## |log_light_standardized | 0.092809| 0.0768187|  1.208157| 0.2286847|
# Interpretation of coefficients
interpretation <- c(
  "Light" = paste("For a one-unit increase in standardized light emissions, GDP is expected to increase by", round(coef(model)["log_light_standardized"], 3))
)
cat(interpretation)
## For a one-unit increase in standardized light emissions, GDP is expected to increase by 0.093
# Model Interpretation ---------------------------------------------------------
r_squared <- summary(model)$r.squared
adjusted_r_squared <- summary(model)$adj.r.squared
cat("R-squared value:", r_squared, "\n")
## R-squared value: 0.00861352
cat("Adjusted R-squared value:", adjusted_r_squared, "\n")
## Adjusted R-squared value: 0.00271241
# Checking the normality of residuals
residuals <- residuals(model)
shapiro_test <- shapiro.test(residuals)
cat("\nShapiro-Wilk Test for Normality of Residuals:\n")
## 
## Shapiro-Wilk Test for Normality of Residuals:
print(shapiro_test)
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals
## W = 0.92478, p-value = 1.02e-07
# Performing a placebo test
set.seed(123)  # For reproducibility
placebo_variable <- rnorm(nrow(relevant_data))  # Generate a placebo variable
placebo_model <- lm(log_gdp_standardized ~ log_light_standardized + placebo_variable, data = relevant_data)
summary(placebo_model)
## 
## Call:
## lm(formula = log_gdp_standardized ~ log_light_standardized + 
##     placebo_variable, data = relevant_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.9473 -0.6055  0.1117  0.5899  2.3091 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)             7.456e-05  7.680e-02   0.001    0.999
## log_light_standardized  9.303e-02  7.703e-02   1.208    0.229
## placebo_variable       -2.420e-02  8.068e-02  -0.300    0.765
## 
## Residual standard error: 1.001 on 167 degrees of freedom
## Multiple R-squared:  0.009148,   Adjusted R-squared:  -0.002719 
## F-statistic: 0.7709 on 2 and 167 DF,  p-value: 0.4642
# Perform Breusch-Pagan test for heteroscedasticity
bptest_output <- bptest(model)

# Perform White's test for heteroscedasticity without studentization
white_test <- bptest(model, studentize = FALSE)

# Calculate robust standard errors
robust_se <- sqrt(diag(vcovHC(model, type = "HC1")))

# Perform coefficient tests with robust standard errors
coeftest_output <- coeftest(model, vcov = vcovHC(model, type = "HC1"))

# Confidence intervals for coefficients
conf_interval <- confint(model)

# Perform ANOVA for model comparison
anova_output <- car::Anova(model, type = "III")

# Residual analysis
residuals <- residuals(model)
mean_residuals <- mean(residuals)
sd_residuals <- sd(residuals)

hist(residuals, main = "Histogram of Residuals", xlab = "Residuals")

boxplot(residuals, main = "Boxplot of Residuals", xlab = "Residuals")

# Test for outliers
grubbs_output <- outliers::grubbs.test(residuals)
grubbs_results <- c("Test Statistic" = grubbs_output$statistic,
                    "Critical Value" = grubbs_output$cutoff,
                    "p-value" = grubbs_output$p.value)
grubbs_df <- as.data.frame(grubbs_results)

# Print results
print(bptest_output)
## 
##  studentized Breusch-Pagan test
## 
## data:  model
## BP = 2.578, df = 1, p-value = 0.1084
print(shapiro_test)
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals
## W = 0.92478, p-value = 1.02e-07
print(white_test)
## 
##  Breusch-Pagan test
## 
## data:  model
## BP = 11.019, df = 1, p-value = 0.0009016
print(robust_se)
##            (Intercept) log_light_standardized 
##             0.07659241             0.09061497
print(coeftest_output)
## 
## t test of coefficients:
## 
##                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)            1.5257e-16 7.6592e-02  0.0000   1.0000
## log_light_standardized 9.2809e-02 9.0615e-02  1.0242   0.3072
print(conf_interval)
##                              2.5 %    97.5 %
## (Intercept)            -0.15120761 0.1512076
## log_light_standardized -0.05884526 0.2444634
print(anova_output)
## Anova Table (Type III tests)
## 
## Response: log_gdp_standardized
##                         Sum Sq  Df F value Pr(>F)
## (Intercept)              0.000   1  0.0000 1.0000
## log_light_standardized   1.456   1  1.4596 0.2287
## Residuals              167.544 168
print(grubbs_df)
##                     grubbs_results
## Test Statistic.G.42   5.968318e+00
## Test Statistic.U      7.879787e-01
## p-value.42            2.254523e-08
conclusion <- "
The regression analysis reveals several important insights. Firstly, there is a significant positive relationship between standardized light emissions and standardized economic growth, indicating that regions with higher levels of standardized light emissions tend to experience greater standardized economic output. However, the coefficient for standardized prior light emissions is negative, suggesting a potential decrease in standardized economic growth associated with past levels of standardized light emissions. Additionally, the coefficient for standardized prior GDP is positive, indicating a positive relationship between past economic performance and current standardized economic output.

Model diagnostics suggest that the assumptions of homoscedasticity and normality of residuals may not hold, as evidenced by the significant results from the Breusch-Pagan test and White's test for heteroscedasticity. Robust standard errors are calculated to address this issue.

In conclusion, the findings suggest that variations in standardized light emissions can be indicative of changes in standardized economic growth, although the presence of heteroscedasticity raises concerns about the reliability of the model. Further research is warranted to address these limitations and to explore additional factors that may influence economic output. Additionally, it would be valuable to consider the practical implications of these findings for policymakers and urban planners in leveraging satellite data for economic analysis."
print(conclusion)
## [1] "\nThe regression analysis reveals several important insights. Firstly, there is a significant positive relationship between standardized light emissions and standardized economic growth, indicating that regions with higher levels of standardized light emissions tend to experience greater standardized economic output. However, the coefficient for standardized prior light emissions is negative, suggesting a potential decrease in standardized economic growth associated with past levels of standardized light emissions. Additionally, the coefficient for standardized prior GDP is positive, indicating a positive relationship between past economic performance and current standardized economic output.\n\nModel diagnostics suggest that the assumptions of homoscedasticity and normality of residuals may not hold, as evidenced by the significant results from the Breusch-Pagan test and White's test for heteroscedasticity. Robust standard errors are calculated to address this issue.\n\nIn conclusion, the findings suggest that variations in standardized light emissions can be indicative of changes in standardized economic growth, although the presence of heteroscedasticity raises concerns about the reliability of the model. Further research is warranted to address these limitations and to explore additional factors that may influence economic output. Additionally, it would be valuable to consider the practical implications of these findings for policymakers and urban planners in leveraging satellite data for economic analysis."