# A tibble: 6 × 24
id price duplex bedrooms baths_total baths_full baths_half stories
<dbl> <dbl> <lgl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 115 369900 TRUE 3 2.5 2 1 2
2 117 375000 TRUE 3 2.5 2 1 2
3 5 769900 FALSE 4 3.5 3 1 2
4 9 699990 FALSE 4 3.5 3 1 2
5 62 436625 FALSE 4 3 3 0 2
6 61 446900 FALSE 3 2.5 2 1 2
# ℹ 16 more variables: subdivision <chr>, sqft <dbl>, year <dbl>, acres <dbl>,
# new <lgl>, style <chr>, garage <dbl>, misc <chr>, pool <lgl>, dock <lgl>,
# fence <lgl>, porch <lgl>, amenities <chr>, golf_course <lgl>,
# fireplace <lgl>, num_fireplace <dbl>
Homework 4 - Visual Solutions
This assignment is intended to give you experience with linear regression. You won’t need to bring in your own datasets—everything you need comes from R packages. Homework datasets and solutions are included in the stat1080r course package, so make sure you have the latest version installed before you begin.
You’ll complete this file by finishing the code and filling in the answer prompts. Replace each #...# with the code needed to answer the question. Sometimes I’ll give you a code “shell” (marked with ##) that you’ll need to uncomment and complete.
When you’re done, submit your .qmd file in Canvas. Be sure it successfully renders to HTML with all your answers showing. Also remember to run lintr on your assignment so that you don’t lose points for style.
There are 3 problems in this assignment (each with several parts), worth a total of 56 points. Don’t forget to check the style guidelines at the end before submitting. Note that you must use the “modern” pipe command |> in your code. Please avoid the use of the old %>% in your code which will result in a loss of points.
Problem 1: House price prediction - data prep (23 points)
Housing appraisal (having the value of your house formally assessed) is a big business and has huge implications for property taxes and mortgages. There are many online datasets that post real-estate prices with the purpose of trying to develop models that can accurately predict a house price based on things a professional appraiser would use to assess the value of a house. In this problem, we plan to look at ways in which we might use linear regression to try and predict house price and begin to explore how regression could help us determine which features of a house are most important when predicting price.
We will be using the housing_hawkes dataset. You can check out the data documentation using ?housing_hawkes in R. The dataset includes real estate prices from 270 house/condo listings in Mount Pleasant, South Carolina in the year 2017.
Part a (2 points)
Before we try creating any regression models, lets first make sure that a linear model is an appropriate way to represent the data. First, remove all rows that have missing values for square footage or price. Second, modify the price variable to be in thousands of dollars (it is currently given in dollars).
Part b (5 points)
Next, create a density plot that looks at the distribution of house prices and a density plot that looks at the distribution of house square footage. Both plots will include three density curves, one for each subdivision.
The following code chunk creates three separate plot themes. One theme places the legend in the upper right corner, one theme places it in the upper left corner, and one theme removes it entirely. You will need to choose between these themes to help you format your plots to look exactly like the provided solution.
In addition to using the provided plot themes, you will also need to change the x and y axis labels, titles, and limits to match the provided solution plot (see homework_4_visual_sol.html). You will also need to use the Dark2 color pallet using the scale_color_brewer() function.
Part c (3 points)
Write a few sentences interpreting the plots. Be sure to address which neighborhoods, if any, seem to have house prices or house square footage that follow a normal distribution. Also address whether or not you think the prices and/or square footage distributions are similar across neighborhoods.
Answer: The distribution of house prices and square footage are NOT the same when comparing the three neighborhoods. The Carolina park neighborhood has a much smaller range of typical house prices and sizes, making me think it is a highly planned neighborhood with “cookie cutter” houses. Prices and square footage in Dunes West and Park West are very right skewed. Carolina Park seems to have prices and square footage values that are closest to following a normal distribution, but there seems to be MORE values near the average (i.e. shorter tails) than we would expect if the data followed a normal distribution.
Part d (2 points)
Linear regression works better when the variables we use in our model are not skewed (though they don’t have to necessarily be normally distributed). Because of this, we are going to see if square footage shares a linear relationship with house price when looking at the natural log-transformation (i.e. log() in R) of both variables. Keeping this in mind, create two new variables that represent the log-transformation of house price and square footage and add them to the dataset. Call these variables log_price and log_sqft respectively.
Part e (3 points)
Now, use the log-transformed variables to make a scatterplot that relates the log-transformed square footage to the log-transformed prices. Do not distinguish the points by neighborhood in this plot. Use the same format and style considerations outlined in Part b to get your version of this plot to match the solution plot exactly.
Part f (2 points)
Based on this plot, do you think it is appropriate to try and create a linear model that uses square footage to predict house price? Explain why or why not.
Answer: I would say that the data do seem to share a linear relationship (i.e. we could fit a straight line through the center of the points). Because of this, it seems appropriate to use linear regression to relate square footage and house price.
Part g (3 points)
Using a similar plot style to Part e and an identical color scheme to Part f, plot the points using color to distinguish subdivisions. Use the geom_smooth(method = "lm") argument to look at estimated regression lines for each neighborhood.
Part h (3 points)
Based on those regression line and their associated confidence intervals (i.e., the gray shaded regions), does it visually appear at the relationship between house price and square footage is the same across all subdivisions? Explain why or why not (using visuals only, no statistical tests at this point).
Answer: The relationship appears to be identical for the Dunes West and Park West subdivisions, but very different for the Carolina Park Subdivision. Carolina Park house prices seem to be less responsive to changes in square footage than the other two neighborhoods.
Problem 2: House price prediction - linear models (15 points)
Regardless of your answer on Problem 1, Part h, we are going to assume that linear regression is an appropriate way to estimate house price using other house or lot features. The following problem parts explore different combinations of variables and methods of prediction.
Part a (2 points)
Use the housing dataset that removed the missing price and square footage values and includes the log-transformed price and square footage variables. Create a simple linear regression model using the lm() function that uses the log-transformed square footage to predict the log-transformed price.
Part b (3 points)
Use the predict() function with your newly created linear model to predict house prices from 1000 sqft to 3000 sqft in increments of 100. Remember that your predictions will be on the log scale, so you will need to use the exp() function to transform your predictions back to the original scale.
Part c (2 points)
Lets now consider a model that includes both square footage and lot size (i.e. acres) as predictors. To do this, we will first need to create a log-transformed version of acres, i.e. log(acres + 0.1). The + 0.1 accommodates the fact that some of the homes have no lot since they are condos, and you cannot take the log of zero.
Add log_acres to your housing dataset and make sure that the new dataset matches the solution exactly.
Part d (3 points)
Use the lm() function to create a linear model that uses log_acres and log_sqft to predict log_price. Confirm that your linear model matches the solution exactly.
Part e (5 points)
Use the confint() function to obtain 95% confidence intervals for all of the model coefficients. We wish to use the confidence intervals to determine if, after accounting for the square footage of the home, lot size is still significantly related to house price. Based on your confidence intervals, what do you conclude and why?
Answer: There is significant evidence to suggest that, after accounting for home square footage, that lot size is linearly related to house price. We can say this because the chance that there is no relationship (i.e. a coefficient the log_acres term equal to 0) is very small based on our sample because our confidence interval does not include zero.
Problem 3: House price prediction - tidy functions (13 points)
Recall from Problem 1 that the relationship between square footage and house price seemed to be different in the Carolina Park subdivision as compared to the other subdivisions. We wish to explore this further by fitting linear models specific to each subdivision.
Part a (5 points)
Use the tidy() function from the broom package to create a dataframe of coefficients (with associated confidence intervals) for three linear models, one for each subdivision, that use log_sqft and log_acres to predict log_price. Remember to use a group_by() operation on an appropriate variable in order to get the tidy() function to give you coefficients for three models instead of one.
Part b (5 points)
Use geom_segment() and facet_wrap() to plot the confidence intervals for the coefficients associated with log_sqft and log_acres. Most of the code is provided, but you need to figure out how to complete the statement for the geom_segment() function. Please consult the function documentation to do this. The result of this code is separate plots for the two variables, with subdivisions provided on the y-axis and the confidence intervals plotted on the x-axis (see the solution plot).
Part c (3 points)
Interpret the results of Part a and b in a few sentences. Do the estimated coefficients appear to be roughly similar to the overall model results? Does your conclusions regarding the statistical significance (or lack thereof) in Problem 2, Part e fail to hold in any of the subdivision-specific models?
Answer: Home prices in Carolina Park seem to be much less responsive to house size as compared to the other two subdivisions. I say this because there is literally no overlap in the confidence intervals associated with coefficient estimates for
log_sqft. The other two subdivisions seem to be similar in all respects, and the coefficient estimates associated with lot size seem to be roughly similar (as evidenced by confidence interval overlap) in all subdivisions.
Submission Style (5 points)
Each submission should have the following elements:
You should submit the
.qmdfile (not theHTMLrendering) with a file name in the style of<last_name>_<first_name>_homework4.qmd.Your
.qmdfile should run without errors when the grader attempts to render it to a html.Your code needs to run “clean” on any lintr checks. Please see the brief videos available on the “Style Guides” tabs of the course canvas page that introduce you to
lintr, andstyler. Note that you can ignore any flags with the tags:trailing_whitespace_linter,trailing_blank_lines_linter, andcommented_code_linter.