# ####################### # # Partial Regression Plot # # ####################### # # OBJECTIVE: # create a partial regression plot in R with price on the vertical axis, # bedrooms on the horizontal axis, and square footage as the control variable. rm(list=ls()) library(readxl) # load data setwd("c:/Documents/Classes/Econ4230/R_and_Stata/") data = read_excel("Data/hprice.xlsx") # original regressions ols_uni = lm(price ~ bedrooms, data = data) ols_multi = lm(price ~ bedrooms + sqrft, data = data) summary(ols_multi) plot(data$bedrooms, data$price, xlab = "number of bedrooms", ylab = "house price ($K)", main = "Unconditional Regression") abline(ols_uni, col="red") # Auxillary Regressions # step 1: Compute the residuals from regressing the price against the control variables aux1 = lm(price ~ sqrft, data = data) # step 2: Compute the residuals from regressing bedrooms against the control variables aux2 = lm(bedrooms ~ sqrft, data = data) # step 3: Regress and plot the residuals from (1) against the residuals from (2). residaux1 = resid(aux1) + mean(data$price) residaux2 = resid(aux2) + mean(data$bedrooms) ols_partial = lm(residaux1 ~ residaux2) summary(ols_partial) plot(residaux2, residaux1, type = "p", pch = 20, main = "Conditional Regression", xlab = "number of bedrooms", ylab = "house price ($K)") abline(ols_partial, col = "red")