# Backward Selection on PSID1976 from the AER package# ---------------------------------------------------# 1. Setup ----------------------------------------------------------------rm(list =ls())# 1a. Install & load necessary packagesif (!requireNamespace("AER", quietly =TRUE)) install.packages("AER")library(AER) # PSID1976 data
Loading required package: car
Loading required package: carData
Loading required package: lmtest
Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
Loading required package: sandwich
Loading required package: survival
if (!requireNamespace("visdat", quietly =TRUE)) install.packages("visdat")library(visdat) # for quick data overviewif (!requireNamespace("stargazer", quietly =TRUE)) install.packages("stargazer")library(stargazer) # for formatted model output
Please cite as:
Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
if (!requireNamespace("MASS", quietly =TRUE)) install.packages("MASS")library(MASS) # for stepAIC()# 2. Load & Preview ------------------------------------------------------data("PSID1976", package ="AER")df <- PSID1976vis_dat(df[1:500, ]) # glimpse first 500 rows
# 3. Select & Clean ------------------------------------------------------# 3a. Pick 12 key variableskeep <-c("hours", # annual hours worked (wife)"wage", # wife's hourly wage"fincome", # family income"education", # wife's years of schooling"age", # wife's age"experience", # wife's labor‐market experience"repwage", # reported wage"hhours", # husband's hours worked"hwage", # husband's wage"youngkids", # children < 6"oldkids", # children 6–18"city"# lives in SMSA)# 3b. Subset & drop NAsdf <- df[, keep]df <-na.omit(df)# 3c. Factorizedf$city <-factor(df$city)# 4. Fit Full Model ------------------------------------------------------full_mod <-lm(hours ~ ., data = df)stargazer(full_mod,type ="text",title ="Full Model: Annual Hours Worked")
The following object is masked from 'package:MASS':
select
The following object is masked from 'package:car':
recode
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(MASS)# 2. Import & Preview Movies Data ------------------------------------------movies_metadata <-read.csv("movies_metadata.csv", stringsAsFactors =FALSE)vis_dat(movies_metadata[1:10000,])
# 3. Clean Movies Data -----------------------------------------------------df <- movies_metadatadf$budget <-as.numeric(df$budget) # coerce to numeric (introduces NAs)
Warning: NAs introduced by coercion
df$overview <-NULL# drop overview columndf$original_language_english <-if_else( df$original_language =="en", 1, 0)df$original_language <-NULLdf$title <-NULL# 4. Regression on Movies Data ---------------------------------------------reg1 <-lm(vote_average ~ vote_count + budget, data = df)stargazer(reg1, type ="text")