Combine these variables in a multiple regression model and analyze the results.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Read and rename files
library(dplyr)
housing_prices <- read.csv("/Users/ohenmaao/Desktop/310/house-sales.csv")
#change name
housing_prices <- housing_prices %>% rename(neighborhood = MUNIDESC)
#remove trailing spces
housing_prices$neighborhood <- trimws(housing_prices$neighborhood)
# covert SALEDATE
housing_prices$SALEDATE <- as.Date(housing_prices$SALEDATE, format = "%m/%d/%y")
# filter to only sales after covid
housing_prices <- housing_prices %>%
filter(SALEDATE > as.Date("2020-06-01"))
vaccines <- read.csv("/Users/ohenmaao/Desktop/310/vaccines.csv")
#change name
vaccines <- vaccines %>% rename(neighborhood = neighborhood_municipality)
covid_cases <- read.csv("/Users/ohenmaao/Desktop/310/covid-cases.csv")
#change name
covid_cases <- covid_cases %>% rename(neighborhood = neighborhood_municipality)
group by neighborhood and calculate summary
#remove empty
housing_prices <- housing_prices %>%
filter(!is.na(PRICE))
#mean price of each neighborhood
housing_prices_summary <- housing_prices %>%
group_by(neighborhood) %>%
summarize(mean_housing_price = mean(PRICE, na.rm = TRUE))
#merge everything
vaccines_summary <- vaccines %>%
group_by(neighborhood) %>%
summarize(total_vaccinations = bivalent_booster)
covid_cases_summary <- covid_cases %>%
group_by(neighborhood) %>%
summarize(total_covid_cases = infections)
merged_data <- merge(housing_prices_summary, covid_cases_summary, by = "neighborhood")
merged_data <- merge(merged_data, vaccines_summary, by = "neighborhood")
muliple linear regression
multi_lm <- lm(mean_housing_price ~ total_covid_cases + total_vaccinations, data = merged_data)
summary(multi_lm)
##
## Call:
## lm(formula = mean_housing_price ~ total_covid_cases + total_vaccinations,
## data = merged_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -179792 -96832 -48626 47069 966401
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 211758.724 20250.671 10.457 <2e-16 ***
## total_covid_cases -6.904 3.884 -1.777 0.0783 .
## total_vaccinations 50.384 26.500 1.901 0.0599 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 166300 on 109 degrees of freedom
## Multiple R-squared: 0.03213, Adjusted R-squared: 0.01437
## F-statistic: 1.809 on 2 and 109 DF, p-value: 0.1687
plot
plot(merged_data$total_covid_cases, merged_data$mean_housing_price)
plot(merged_data$total_vaccinations, merged_data$mean_housing_price)
plot(merged_data$total_covid_cases, merged_data$total_vaccinations)
Which variables are significant? Both total covid cases and total vaccinations have high p values, greater than 0.05 which indicates that they are not statistically significant.
What is the model fit? The multiple R-squared value is 0.03213. This means that total covid cases and total vaccinations only explain 3.21% of the data in housing prices.
Residual error difference between different models A residual error of 166300 is high, suggesting that the model has a high amount of unexplained variability. It might be lower when looking at simple regression models.
Insights from R-squared error Adjusted R-squared is even lower than R-squared, again meaning total covid cases and total vaccinations may not be strong variables in explaining housing prices.