Step1 Project description and setup

The analysis is for the house price estimation for the Westwood, a suburb in Massachusetts.

library(tidyverse)

uniform1 <- runif(10000)
uniform2 <- runif(10000)

dt_dist_mean = 4.2
dt_dist_sd = 0.7

sq_feet_mean = 1500
sq_feet_sd = 150

pct_red = 0.2
pct_blue = 0.65
pct_green = 0.15

pct_pool = 0.15
pct_no_pool = 0.85

input_coeffs = list(Constant = 100,
                    `Effect of square feet` = 0.7,
                    `Effect of distance` = -50,
                    `Effect of blue` = 100,
                    `Effect of green` = 150,
                    `Effect of pool` = 200)

Step2 Data generation

Generating the simulated data set.

data <- tibble(sq_feet = rnorm(10000,
                               mean = sq_feet_mean,
                               sd = sq_feet_sd),
               dt_dist = rnorm(10000,
                               mean = dt_dist_mean,
                               sd = dt_dist_sd),
               uniform1 = uniform1,
               uniform2 = uniform2) |>
  mutate(color = case_when(uniform1 < pct_red ~ "Red",
                           uniform1 < pct_red + pct_blue ~ "Blue",
                           TRUE ~ "Green"),
         pool = ifelse(uniform2 < pct_pool, 1, 0)) |>
  mutate(blue = ifelse(color == "Blue", 1, 0),
         green = ifelse(color == "Green", 1, 0)) |>
  mutate(rent = input_coeffs$Constant +
           input_coeffs$`Effect of square feet` * sq_feet +
           input_coeffs$`Effect of distance` * dt_dist +
           input_coeffs$`Effect of blue` * blue +
           input_coeffs$`Effect of green` * green +
           input_coeffs$`Effect of pool` * pool)

Step3 Data visualization

Visualizing the simulated data set.

Step3.1 Distribution of continuous variables

A histogram showing the distribution of each continuous variable (orange for the predictor variable and pink for the outcome variable)

  ggplot(data) +
    geom_histogram(aes(x = sq_feet),
                 bins = 50,
                 fill = "orange",
                 color = "white") +
    scale_x_continuous(name = "Square feet") +
    scale_y_continuous(name = "Frequency") +
    theme_linedraw()

  ggplot(data) +
    geom_histogram(aes(x = dt_dist),
                 bins = 50,
                 fill = "orange",
                 color = "white") +
    scale_x_continuous(name = "Distance to downtown") +
    scale_y_continuous(name = "Frequency") +
    theme_linedraw()

  ggplot(data) +
    geom_histogram(aes(x = rent),
                 bins = 50,
                 fill = "pink",
                 color = "white") +
    scale_x_continuous(name = "Monthly rent") +
    scale_y_continuous(name = "Frequency") +
    theme_linedraw()

Step3.2 Relationship of continuous variable with the outcome

A scatterplot showing the relationship between each continuous predictor variable and the outcome variable.

  ggplot(data) +
    geom_point(aes(x = sq_feet,
                   y = rent),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_continuous(name = "Square feet") +
    scale_y_continuous(name = "Monthly rent") +
    theme_linedraw()

  ggplot(data) +
    geom_point(aes(x = dt_dist,
                   y = rent),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_continuous(name = "Distance to downtown") +
    scale_y_continuous(name = "Monthly rent") +
    theme_linedraw()

Step3.3 Relationship of categorical variable with the outcome

A scatterplot showing the relationship between each categorical predictor variable and the outcome variable.

  ggplot(data) +
    geom_jitter(aes(x = pool,
                   y = rent),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_continuous(name = "Pool",
                       breaks = c(0,1),
                       labels = c("No", "Yes")) +
    scale_y_continuous(name = "Monthly rent") +
    theme_linedraw()

  ggplot(data) +
    geom_jitter(aes(x = color,
                   y = rent),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_discrete(name = "Color",
                     labels = c("Blue", "Green", "Red")) +
    scale_y_continuous(name = "Monthly rent") +
    theme_linedraw()

Step4 Descriptive statistics

Calculate the mean and standard deviation of each continuous variable. For the predictor variables, compare the calculated mean and standard deviations to the values used to generate the variables.

Estimate a regression model predicting the outcome variable from the predictor variables. Compare your regression coefficients to the coefficients you used to generate the outcome variable.

Step4.1 The mean and standard deviation of each continuous variable and comparisons

The following table shows the descriptive statistics for the outcome variable.

  library(knitr) 

  summary_outcome <- data |>
    summarize(`Calculated average monthly rent` = mean(rent),
              `Calculated standard deviation of monthly rent` = sd(rent))

kable(summary_outcome,
      format.arcgis = list(digits = 6))
Calculated average monthly rent Calculated standard deviation of monthly rent
1058.479 140.7915

The following table shows the average values of the continuous predictor variables to the values that were used to generate the data.

  mean_summary <- data |>
    pivot_longer(cols = c("sq_feet","dt_dist"),
                 names_to = "Variable",
                 values_to = "value") |>
  group_by(Variable) |>
  summarize(`Calculated average` = mean(value)) |>
  mutate(`Input average` = case_when(Variable == "dt_dist" ~ dt_dist_mean,
                                     Variable == "sq_feet" ~ sq_feet_mean)) |>
  mutate(Variable = case_when(Variable == "dt_dist" ~
                                "Distance to downtown",
                              Variable == "sq_feet" ~
                                "Square footage")) |>
  mutate(Difference = `Input average` - `Calculated average`)

kable(mean_summary,
      format.args = list(digits = 4),
      align = c("l", "c", "c", "c"))
Variable Calculated average Input average Difference
Distance to downtown 4.203 4.2 -0.003145
Square footage 1501.044 1500.0 -1.044128

The following table compares the standard deviations of the continuous predictor variables that were used to generate the data.

  sd_summary <- data |>
    pivot_longer(cols = c("sq_feet","dt_dist"),
                 names_to = "Variable",
                 values_to = "value") |>
  group_by(Variable) |>
  summarize(`Calculated standard deviation` = sd(value)) |>
  mutate(`Input standard deviation` = 
           case_when(Variable == "dt_dist" ~ dt_dist_sd,                                     Variable == "sq_feet" ~ sq_feet_sd)) |>
  mutate(Variable = case_when(Variable == "dt_dist" ~ "Distance to downtown",
              Variable == "sq_feet" ~ "Square footage")) |>
  mutate(Difference = `Input standard deviation` - `Calculated standard deviation`)

kable(sd_summary,
      format.args = list(digits = 4),
      align = c("l", "c", "c", "c"))
Variable Calculated standard deviation Input standard deviation Difference
Distance to downtown 0.7005 0.7 -0.0004945
Square footage 149.6599 150.0 0.3401167

Step4.2 Regression model and the comparisons

Linear regression model summary

  model <- lm(rent ~
                sq_feet +
                dt_dist +
                blue +
                green +
                pool,
              data = data)
summary(model)
## 
## Call:
## lm(formula = rent ~ sq_feet + dt_dist + blue + green + pool, 
##     data = data)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.208e-11 -9.200e-14 -1.500e-14  5.400e-14  2.254e-10 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.000e+02  2.680e-13  3.731e+14   <2e-16 ***
## sq_feet      7.000e-01  1.513e-16  4.628e+15   <2e-16 ***
## dt_dist     -5.000e+01  3.232e-14 -1.547e+15   <2e-16 ***
## blue         1.000e+02  5.821e-14  1.718e+15   <2e-16 ***
## green        1.500e+02  7.853e-14  1.910e+15   <2e-16 ***
## pool         2.000e+02  6.312e-14  3.168e+15   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.263e-12 on 9994 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 7.738e+30 on 5 and 9994 DF,  p-value: < 2.2e-16

The following table compares the regression coefficients to the values that were used to generate the outcome variable.

  model <- lm(rent ~
                sq_feet +
                dt_dist +
                blue +
                green +
                pool,
              data = data)
  coeff_summary = tibble(Variable = names(input_coeffs),
                         `Input coefficients` = input_coeffs,
                        `Calculated coefficients` = model$coefficients) |>
    mutate(Difference = as.numeric(`Input coefficients`) -
             as.numeric(`Calculated coefficients`))

kable(coeff_summary)
Variable Input coefficients Calculated coefficients Difference
Constant 100 100.0 0
Effect of square feet 0.7 0.7 0
Effect of distance -50 -50.0 0
Effect of blue 100 100.0 0
Effect of green 150 150.0 0
Effect of pool 200 200.0 0