Load in packages

library(ggplot2)
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Then need to read in the data

price = read.csv("homeprice.csv")

Explore the relationship between sale price and the other variables

Sale Price and List Price:

ggplot(price, aes(x = list, y = sale)) + geom_point() + xlab("List Price") + ylab("Sale Price") + ggtitle("Sale Price & List Price")

flist = factor(price$list)
ggplot(price, aes(x = flist, y = sale)) + geom_boxplot() + xlab("List Price") + ylab("Sale Price") + ggtitle("Boxplot Sale Price & List Price")

Sale Price and Full Bathrooms:

ggplot(price, aes(x = full, y = sale)) + geom_point() + xlab("# of Full Bathrooms") + ylab("Sale Price") + ggtitle("Sale Price & # of Full Bathrooms")

ffull = factor(price$full)
ggplot(price, aes(x = ffull, y = sale)) + geom_boxplot() + xlab("# of Full Bathrooms") + ylab("Sale Price") + ggtitle("Boxplot Sale Price & # of Full Bathrooms")

ggplot(price, aes(x = sale, fill = ffull)) + geom_histogram(binwidth = 18, color = "white") + xlab("Sale Price") + ylab("# of Full Bathrooms") + ggtitle("Histogram Sale Price & # of Full Bathrooms")

Sale Price and Half Bathrooms:

ggplot(price, aes(x = half, y = sale)) + geom_point() + xlab("# of Half Bathrooms") + ylab("Sale Price") + ggtitle("Sale Price & # of Half Bathrooms")

fhalf = factor(price$half)
ggplot(price, aes(x = fhalf, y = sale)) + geom_boxplot() + xlab("# of Half Bathrooms") + ylab("Sale Price") + ggtitle("Boxplot Sale Price & # of Half Bathrooms")

ggplot(price, aes(x = sale, fill = fhalf)) + geom_histogram(binwidth = 18, color = "white") + xlab("Sale Price") + ylab("# of Half Bathrooms") + ggtitle("Histogram Sale Price & # of Half Bathrooms")

Sale Price and Bedrooms:

ggplot(price, aes(x = bedrooms, y = sale)) + geom_point() + xlab("# of Bedrooms") + ylab("Sale Price") + ggtitle("Sale Price & # of Bedrooms")

fbedrooms = factor(price$bedrooms)
ggplot(price, aes(x = fbedrooms, y = sale)) + geom_boxplot() + xlab("# of Bedrooms") + ylab("Sale Price") + ggtitle("Boxplot Sale Price & # of Bedrooms")

ggplot(price, aes(x = sale, fill = fbedrooms)) + geom_histogram(binwidth = 18, color = "white") + xlab("Sale Price") + ylab("# of Bedrooms") + ggtitle("Histogram Sale Price & # of Bedrooms")

Sale Price and Rooms:

ggplot(price, aes(x = rooms, y = sale)) + geom_point() + xlab("# of Rooms") + ylab("Sale Price") + ggtitle("Sale Price & # of Rooms")

frooms = factor(price$rooms)
ggplot(price, aes(x = frooms, y = sale)) + geom_boxplot() + xlab("# of Rooms") + ylab("Sale Price") + ggtitle("Boxplot Sale Price & # of Rooms")

ggplot(price, aes(x = sale, fill = frooms)) + geom_histogram(binwidth = 18, color = "white") + xlab("Sale Price") + ylab("# of Rooms") + ggtitle("Histogram Sale Price & # of Rooms")

Sale Price and Neighborhood:

ggplot(price, aes(x = neighborhood, y = sale)) + geom_point() + xlab("Neighborhood") + ylab("Sale Price") + ggtitle("Sale Price & Neighborhood")

fneighborhood = factor(price$neighborhood)
ggplot(price, aes(x = fneighborhood, y = sale)) + geom_boxplot() + xlab("Neighborhood") + ylab("Sale Price") + ggtitle("Boxplot Sale Price & Neighborhood")

ggplot(price, aes(x = sale, fill = fneighborhood)) + geom_histogram(binwidth = 18, color = "white") + xlab("Sale Price") + ylab("Neighborhood") + ggtitle("Histogram Sale Price & Neighborhood")

Identify those variables that appear to have the strongest relationship with sale price:

Just off how they appear, sale price and list price appear to have the strongest relationship.

Part 2:

Build a multiple linear regression model to explain the sale price:

sp = lm(sale ~ full + half + bedrooms + rooms + neighborhood, data = price)
summary(sp)
## 
## Call:
## lm(formula = sale ~ full + half + bedrooms + rooms + neighborhood, 
##     data = price)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -59.31 -34.06   7.20  21.32  55.93 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -135.263     37.283  -3.628  0.00141 ** 
## full           26.225     13.896   1.887  0.07181 .  
## half           43.242     12.830   3.370  0.00264 ** 
## bedrooms       20.409     17.798   1.147  0.26329    
## rooms           6.488     10.383   0.625  0.53823    
## neighborhood   77.243     10.077   7.665 8.86e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39.29 on 23 degrees of freedom
## Multiple R-squared:  0.9079, Adjusted R-squared:  0.8879 
## F-statistic: 45.34 on 5 and 23 DF,  p-value: 3.686e-11
plot(sp, which = 1)

hist(residuals(sp))

anova(sp)
## Analysis of Variance Table
## 
## Response: sale
##              Df Sum Sq Mean Sq F value    Pr(>F)    
## full          1 151632  151632 98.2101 9.062e-10 ***
## half          1  87430   87430 56.6271 1.206e-07 ***
## bedrooms      1  10581   10581  6.8530   0.01538 *  
## rooms         1   9632    9632  6.2387   0.02009 *  
## neighborhood  1  90717   90717 58.7562 8.859e-08 ***
## Residuals    23  35511    1544                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Based on the anova, it appears that the variable with the greatest effect on sale price is the # of full bathrooms. This is followed by the neighborhood.

Part 3:

Build a second model using the same variables to explain the list price:

lp = lm(list ~ full + half + bedrooms + rooms + neighborhood, data = price)
summary(lp)
## 
## Call:
## lm(formula = list ~ full + half + bedrooms + rooms + neighborhood, 
##     data = price)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -60.788 -28.776   4.351  23.859  62.720 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -144.544     36.026  -4.012 0.000546 ***
## full           32.125     13.427   2.392 0.025293 *  
## half           45.556     12.397   3.675 0.001257 ** 
## bedrooms       18.446     17.197   1.073 0.294572    
## rooms           7.126     10.033   0.710 0.484661    
## neighborhood   77.430      9.737   7.952 4.75e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 37.97 on 23 degrees of freedom
## Multiple R-squared:  0.9183, Adjusted R-squared:  0.9006 
## F-statistic: 51.74 on 5 and 23 DF,  p-value: 9.358e-12
plot(lp, which = 1)

hist(residuals(lp))

anova(lp)
## Analysis of Variance Table
## 
## Response: list
##              Df Sum Sq Mean Sq  F value    Pr(>F)    
## full          1 169594  169594 117.6457 1.615e-10 ***
## half          1  92249   92249  63.9922 4.294e-08 ***
## bedrooms      1   9745    9745   6.7597   0.01601 *  
## rooms         1  10162   10162   7.0494   0.01415 *  
## neighborhood  1  91158   91158  63.2352 4.754e-08 ***
## Residuals    23  33156    1442                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Based on the anova, it appears that the variable with the greatest effect on list price is the # of full bathrooms.

This is followed by the # of half bathrooms.

Are there differences from the sale price?

Not significant difference, however this is some change in the relationships.

Could you use this information to recommend which characteristic of a house a real estate agent should concentrate on?

Yes you could, it seems that focusing on the number of full bathrooms and the neighborhood would be most advisable given they have the strongest relationship with sale price.

Part 4:

Effect of neighborhood on the difference between sale price and list price?

mprice <- price %>% 
  mutate(pd = sale - list)
ndiff <- lm(pd ~ neighborhood, data = mprice)
summary(ndiff)
## 
## Call:
## lm(formula = pd ~ neighborhood, data = mprice)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -30.05  -7.50  -0.85   5.80  33.05 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept)     7.800      7.435   1.049    0.303
## neighborhood   -3.150      2.428  -1.298    0.205
## 
## Residual standard error: 13 on 27 degrees of freedom
## Multiple R-squared:  0.0587, Adjusted R-squared:  0.02383 
## F-statistic: 1.684 on 1 and 27 DF,  p-value: 0.2054

Do richer neighborhoods mean it is more likely to have a house go over the asking price?

No, richer neighborhoods does not mean it is more likely to go over asking price.