# Load necessary libraries
library(AER)
library(haven)
library(tidyverse)
# Define a function to read data from a specified URL
read_data <- function(df) {
# Construct the full URL
full_path <- paste("https://github.com/scunning1975/mixtape/raw/master/", df, sep = "")
# Read the .dta file from the URL
df <- read_dta(full_path)
return(df)
}
# Use the function to read the 'card.dta' dataset
card <- read_data("card.dta")
# Attach the dataframe to make its columns directly accessible as variables
attach(card)
# Define the variables for the regression analyses
Y1 <- lwage # Dependent variable
Y2 <- educ # Endogenous variable
X1 <- cbind(exper, black, south, married, smsa) # Exogenous variables
X2 <- nearc4 # Instrument
# Perform an OLS regression
ols_reg <- lm(Y1 ~ Y2 + X1) # Y1 is the dependent variable, Y2 and X1 are the independent variables
summary(ols_reg) # Display the results of the OLS regression
# Perform a 2SLS regression
iv_reg = ivreg(Y1 ~ Y2 + X1 | X1 + X2) # Y1 is the dependent variable, Y2 and X1 are the independent variables, X1 and X2 are the instruments
summary(iv_reg) # Display the results of the 2SLS regression