Background

This is a real dataset of house prices sold in Seattle, WA, USA between August and December 2022.

Data Description

The dataset consists of a dataframe with 1665 observations with the following 6 variables:

1.beds = Number of bedrooms in property 2. region = based on zip code, north, central or south Seattle. 3. baths = Number of bathrooms in property. Note 0.5 corresponds to a half-bath which has a sink and toilet but no tub or shower 4. size = Total floor area of property in square feet 5. lot_size = Total area of the land where the property is located on in square feet 6. price = Price the property was sold for (US dollars) ##THIS IS THE RESPONSE VARIABLE

Source: Kaggle

Instructions on reading the data

To read the data in R, save the file in your working directory (make sure you have changed the directory if different from the R working directory) and read the data using the R function read.csv()

# reading the dataset
setwd('/Users/ellio/Downloads')
price_data <- read.csv("SeattleHousing.csv")

# Show the first three rows of data
head(price_data ,3)
##   beds  Region baths size lot_size  price
## 1    1 Central     1  704      500 645000
## 2    3 Central     3 1524      513 850000
## 3    3 Central     3 1524      513 875000

Exploratory Data Analysis

# Compute univariate numeric summary statistics
summary(price_data)
##       beds           Region              baths            size      
##  Min.   : 1.000   Length:1665        Min.   :0.500   Min.   :  376  
##  1st Qu.: 2.000   Class :character   1st Qu.:1.500   1st Qu.: 1260  
##  Median : 3.000   Mode  :character   Median :2.000   Median : 1720  
##  Mean   : 3.126                      Mean   :2.298   Mean   : 1896  
##  3rd Qu.: 4.000                      3rd Qu.:3.000   3rd Qu.: 2360  
##  Max.   :15.000                      Max.   :9.000   Max.   :11010  
##     lot_size          price        
##  Min.   :   500   Min.   : 159488  
##  1st Qu.:  2734   1st Qu.: 680000  
##  Median :  5000   Median : 865000  
##  Mean   :  9673   Mean   :1010483  
##  3rd Qu.:  7350   3rd Qu.:1175000  
##  Max.   :400752   Max.   :6250000
# Univariate Charts--histograms for the quantitative variables
hist(price_data $beds)

hist(price_data $baths)

hist(price_data $size)

hist(price_data $lot_size)

hist(price_data $price)

Compute the correlation coefficient for each quantitative predicting variables against the response

#Create the correlation matrix for the quantitative variables  CHANGE THE DATA TABLE NAME
round(cor(price_data [,-2]),2)
##           beds baths  size lot_size price
## beds      1.00  0.59  0.73    -0.13  0.46
## baths     0.59  1.00  0.62    -0.08  0.54
## size      0.73  0.62  1.00    -0.07  0.74
## lot_size -0.13 -0.08 -0.07     1.00 -0.09
## price     0.46  0.54  0.74    -0.09  1.00

Describe the strength and direction of the top 3 predicting variables that have the strongest linear relationships with the response.

Size has the strongest correlation with Price at 0.74. It’s a positive correlation, and it’s the closest to the perfect positive correlation out of all 20 correlative relationships.

The number of baths has the second strongest correlation with Price at 0.54. It’s a positive correlation, and the relationship has a moderate to weak correlation.

The number of beds has the third strongest correlation with Price at 0.46; The relationship has a positive correlation and has a moderate correlation to price like the number of baths, although weaker in comparison.

All three of these relationships are positive correlations; surprisingly, the “strongest” negative correlation that Price has with a variable isn’t strong with a correlation with -0.09 (lot size and Price), but it’s likely because they don’t have a quantifiable relationship (the quantity of items [lot size] doesn’t increase the price of a house).

Create a boxplot of the qualitative predicting variable versus the response variable.

# Use the qualitative variable with one of the response variables to create a box plot
boxplot(price_data $price ~ price_data $Region )

Explain the relationship between the qualitative predicting variable versus the response variable.

Observing the box plots for each reason, the Central region has the most expensive housing prices; the median for all regions was similar (but may not appear that way realistically; the box plots were scaled down because of the gap between the minimum and maximum housing prices), but the North and South regions’ data plots are more spread out in comparison to the Central region.

Create boxplots of the qualitative predicting variable versus the quantitative PREDICTOR variables

# Box plots by vehicle type  CHANGE THE VARIABLES AND DATA TABLE
#par(mfrow = c(2, 2))
boxplot(price_data $beds ~ price_data $Region)

boxplot(price_data $size ~ price_data $Region)

boxplot(price_data $baths ~ price_data $Region)

boxplot(price_data $lot_size ~ price_data $Region)

Explain the relationship between the variables.

When comparing the statistics for the number of beds in each region, the median is the same for each region; the plots visibly show that each region nearly matches each others’ data outliers for the number of beds.

The size is most spread out in the Central Region, but the medians are strikingly similar for all regions, just like the data for bed quantity. The data is most compacted in the data points in the South Region.

The medians also match for every region in their relationship between the number of baths in each region. The data points nearly replicate for every region, with the exception of a few outliers. The South region also happens to be the only region with a first quantile data statistic.

Create scatterplots of the response variable against each quantitative predicting variable Describe the general trend of each plot.

## Explore the relationships between all predictor variables and the response
plot(price_data$beds,price_data$price,xlab = "Beds", ylab = "Housing Prices")

plot(price_data$size,price_data$price,xlab = "Size", ylab = "Housing Prices")

plot(price_data$baths,price_data$price,xlab = "Baths", ylab = "Housing Prices")

plot(price_data$lot_size,price_data$price,xlab = "Lot Size", ylab = "Housing Prices")

Describe the general trend of each plot.

Housing prices and the number of beds a positive moderate correlation that does not appear to have a linear trend; a wider range of values shows when a house has two beds.

The plot comparing housing prices and size shows a strong positive trend; the data is more linear from a size of approximately 400 feet to a size of 3000 feet.

The plot between the number of baths and housing prices also shows a non-linear, positively moderate trend. The widest range of values comes in houses between 5-6 baths (there is not supposed to be a fraction of a bath.)

The plot between lot size and housing prices has a weak negative correlation; the range of prices is greatest in between the interval 0e+00 and 1e+05 (which indicate the size lot by hundred thousands).

Regression Model Fitting and Interpretation

Fit a multiple linear regression model called model1 using with the response and the top 2 predicting variables with the strongest relationship with the response variable

# Fit a linear regression model  CHANGE THE VARIABLES AND DATA TABLE

model1 <- lm(price ~ size + baths, data = price_data)

summary(model1)
## 
## Call:
## lm(formula = price ~ size + baths, data = price_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1512769  -184967   -14583   139168  4682870 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 54645.49   24309.14   2.248   0.0247 *  
## size          412.82      12.88  32.043  < 2e-16 ***
## baths       75416.75   11711.01   6.440 1.56e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 377700 on 1662 degrees of freedom
## Multiple R-squared:  0.5643, Adjusted R-squared:  0.5637 
## F-statistic:  1076 on 2 and 1662 DF,  p-value: < 2.2e-16

Using alpha = 0.05, which of the estimated coefficients are statistically significant?

Both coefficients are statistically significant.

Is the overall regression significant? And why or why not?

Yes; the p-value for the f-statistic is very small.

What is the estimated coefficient for the intercept? Interpret this coefficient in the context of the dataset.

The estimated coefficient for the intercept is 54645.49; what this means is that with a size of 0 and no baths inside of a given house, the price of that house would be $54645.49.

What is the estimated coefficient for the strongest correlated predictor variable? Interpret this coefficient in the context of the dataset.

The estimated coefficient for the strongest correlated predictor variable is 412.82. It means that given the fixed value of the number of baths, an increase of one square foot would equal an increase of $412.82 in housing prices.

Goodness of Fit

# Extract the standardized residuals
resids = rstandard(model1)
fits = model1$fitted

# Constant Variance Assumption
plot(fits, resids,
     xlab="Fitted Values",
     ylab="Residuals",
     main="")
abline(0, 0, lty=2, lwd=2)

Comment on whether the plot shows constant variance.

The plot does not show variance because the spread of the residuals changes throughout the graph; the fitted values not only stick on the same side of the residual graph at certain fitting values, but the residual size changes throughout the graph. The residuals become smaller from 0 to 600 fitted values, and the residual sizes expand after the plot reaches 600 fitted values.

Fit a linear regression model called model2 with response variable and the qualitataive variable as the predicting variable

# CHANGE THE VARIABLES AND DATA TABLE
model2 <- lm (price ~ Region, price_data )
summary(model2)
## 
## Call:
## lm(formula = price ~ Region, data = price_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -976039 -305368 -122368  149942 5023961 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1226039      26408  46.426  < 2e-16 ***
## RegionNorth  -193671      33439  -5.792 8.31e-09 ***
## RegionSouth  -425981      35779 -11.906  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 548900 on 1662 degrees of freedom
## Multiple R-squared:  0.07961,    Adjusted R-squared:  0.0785 
## F-statistic: 71.87 on 2 and 1662 DF,  p-value: < 2.2e-16

What is the model for each of the values of the qualitataive variable?

RegionNorth Y = 1226039 - 193671 RegionSouth Y = 1226039 - 425981 RegionCentral Y = 1226039

Goodness of Fit

# Extract the standardized residuals
resids2 = rstandard(model2)
fits2 = model2$fitted

# Constant Variance Assumption
plot(fits2, resids2,
     xlab="Fitted Values",
     ylab="Residuals",
     main="")
abline(0, 0, lty=2, lwd=2)

Does the fitted values vs. standardized residuals plot indicate constant variance?

They do not indicate constant variance; for all the fitted values on the comparison plot, the residuals start becoming larger and more uneven (including the data plots in between 470 and 500 fitted values) .

Fit a multiple linear regression model called model3 using the response variable and your choice of predictor variables.

# CHANGE THE VARIABLES AND DATA TABLE
model3 <- lm (price ~  beds + Region + size + baths + lot_size, price_data)

summary(model3)
## 
## Call:
## lm(formula = price ~ beds + Region + size + baths + lot_size, 
##     data = price_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1746369  -170383   -24673   139110  4363463 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.219e+05  2.966e+04  10.851  < 2e-16 ***
## beds        -8.873e+04  1.124e+04  -7.893 5.33e-15 ***
## RegionNorth -1.045e+05  2.165e+04  -4.828 1.51e-06 ***
## RegionSouth -2.829e+05  2.344e+04 -12.073  < 2e-16 ***
## size         4.730e+02  1.477e+01  32.015  < 2e-16 ***
## baths        9.180e+04  1.134e+04   8.093 1.12e-15 ***
## lot_size    -9.015e-01  3.133e-01  -2.877  0.00406 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 350900 on 1658 degrees of freedom
## Multiple R-squared:  0.6246, Adjusted R-squared:  0.6233 
## F-statistic: 459.9 on 6 and 1658 DF,  p-value: < 2.2e-16

Using alpha = 0.05, which of the estimated coefficients are statistically significant in model2?

All of the estimated coefficients are statistically significant.

Interpret the estimated coefficient for RegionNorth in the context of the dataset.

Assuming all of the other variables are fixed, the coefficient for RegionNorth in the context of the dataset is -282900, which means that the housing price in a given house in RegionNorth is $282,900 less compared to that of the average housing price in a house in RegionCentral.

This means that for two houses in RegionCentral and RegionNorth with the same size, number of baths, and lot size, the house in in RegionNorth will have a cheaper housing price.

Is the overall regression (model3) significant at an alpha-level of 0.05? Explain how you determined the answer.

Yes, the regression is statistically significant because the f-statistic’s p-value is very small; the p-value is less than 2.2e-16, which is much smaller comparing it to 0.05.

Goodness of Fit

# Extract the standardized residuals
resids3 = rstandard(model3)
fits3 = model3$fitted

# Constant Variance Assumption
plot(fits3, resids3,
     xlab="Fitted Values",
     ylab="Residuals",
     main="")
abline(0, 0, lty=2, lwd=2)

Does the fitted values vs. standardized residuals plot indicate constant variance?

No, the plot doesn’t indicate constant variance because most of the data points fall on only one side of the data line at nearly all of the fitted values; the residual points are not only uneven throughout the graph, but the residuals get larger as the number of fitted values increase.

What are the Adjusted R^2 values for model 1 and model 3?

# Return the R^2 values for model 1 and model 3
paste("Model 1 adjusted R^2:", round(summary(model1)$adj.r.squared,2))
## [1] "Model 1 adjusted R^2: 0.56"
paste("Model 3 adjusted R^2:",round(summary(model3)$adj.r.squared,2))
## [1] "Model 3 adjusted R^2: 0.62"

How would you describe the performance of model 1 and model 3 based on the R^2 value.

Based on the R^2 value of model 1 and model 3, Model 3 has a greater R^2 value than Model 1 (0.94 compared to 0.93); the difference indicates that Model 3 has 1% more variability in housing prices compared to Model 1.

Prediction

Using model3, predict the mean response for 2 sets of characteristics:

CHANGE THE VARIABLES AND THE VALUES TO MATCH YOUR DATA SET

  1. Type=“Central”, beds=3, size=2400, baths=3 lot_size=4000

  2. Type=“North”, beds=3, size=2400, lot_size=4000

#new data
# CHANGE THE VARIABLES AND DATA TABLE AND VALUES

newvals1 <- data.frame(Region="Central", beds=3, size=2400, baths=3, lot_size=4000)
newvals2 <- data.frame(Region="North", beds=3, size=2400, baths=3, lot_size=4000)

# Create a new set of data for a dream house mansion in Central Seattle
dreamhouse1 <- data.frame(Region="Central", beds=5, size=6000, baths=5, lot_size=15000)

# Repeat the same process but use new numbers for a housing block in South Seattle
southhouse1 <- data.frame(Region="South", beds=4, size=1500, baths=4, lot_size=800)


# Confidence Interval for the response variable
predict(model3,newvals1,interval='confidence',level=.95)
##       fit     lwr     upr
## 1 1462609 1426195 1499024
predict(model3,newvals2,interval='confidence',level=.95)
##       fit     lwr     upr
## 1 1358115 1325444 1390785
predict(model3,dreamhouse1,interval='confidence',level=.95)
##       fit     lwr     upr
## 1 3161535 3071824 3251246
predict(model3,southhouse1,interval='confidence',level=.95)
##        fit      lwr      upr
## 1 759963.9 704963.2 814964.6

What is the predicted mean response for set 1?

$1,462,609.00

What is the predicted mean price for set 2?

$1,358,115.00

What is the predicted mean price for the dream house?

$3,161,535.00

What is the predicted mean price for the housing block?

$759,963.90