Step1 Project description and setup

The analysis is looking at the degree of social isolation for the suburban areas, the case take place at Westwood, a suburb located south-east to Boston. The criteria investigated are destination towards MBTA station, house area, whether there are balconies with terraces and the family status.

library(tidyverse)

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

mbta_dist_mean = 4.2
mbta_dist_sd = 0.7

sq_feet_mean = 1500
sq_feet_sd = 150

fsts_single = 0.2
fsts_married = 0.65
fsts_divorced = 0.15

hst_terrace = 0.15
hst_no_terrace = 0.85

input_coeffs = list(Constant = 100,
                    `Effect of square feet` = 0.7,
                    `Effect of distance` = -50,
                    `Effect of married` = 100,
                    `Effect of divorced` = 150,
                    `Effect of terrace` = 200)

Step2 Data generation

Generating the simulated data set.

data <- tibble(sq_feet = rnorm(10000,
                               mean = sq_feet_mean,
                               sd = sq_feet_sd),
               mbta_dist = rnorm(10000,
                               mean = mbta_dist_mean,
                               sd = mbta_dist_sd),
               uniform1 = uniform1,
               uniform2 = uniform2) |>
  mutate(color = case_when(uniform1 < fsts_single ~ "single",
                           uniform1 < fsts_single + fsts_married ~ "married",
                           TRUE ~ "divorced"),
         terrace = ifelse(uniform2 < hst_terrace, 1, 0)) |>
  mutate(married = ifelse(color == "married", 1, 0),
         divorced = ifelse(color == "divorced", 1, 0)) |>
  mutate(social_isolation = input_coeffs$Constant +
           input_coeffs$`Effect of square feet` * sq_feet +
           input_coeffs$`Effect of distance` * mbta_dist +
           input_coeffs$`Effect of married` * married +
           input_coeffs$`Effect of divorced` * divorced +
           input_coeffs$`Effect of terrace` * terrace)

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 = mbta_dist),
                 bins = 50,
                 fill = "orange",
                 color = "white") +
    scale_x_continuous(name = "Distance to mbta") +
    scale_y_continuous(name = "Frequency") +
    theme_linedraw()

  ggplot(data) +
    geom_histogram(aes(x = social_isolation),
                 bins = 50,
                 fill = "pink",
                 color = "white") +
    scale_x_continuous(name = "Social isolation scale") +
    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 = social_isolation),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_continuous(name = "Square feet") +
    scale_y_continuous(name = "Social isolation scale") +
    theme_linedraw()

  ggplot(data) +
    geom_point(aes(x = mbta_dist,
                   y = social_isolation),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_continuous(name = "Distance to mbta") +
    scale_y_continuous(name = "Social isolation scale") +
    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 = terrace,
                   y = social_isolation),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_continuous(name = "Terrace",
                       breaks = c(0,1),
                       labels = c("No", "Yes")) +
    scale_y_continuous(name = "Social isolation scale") +
    theme_linedraw()

  ggplot(data) +
    geom_jitter(aes(x = color,
                   y = social_isolation),
                   color = "orange",
                   alpha = 0.25) +
    scale_x_discrete(name = "Family status",
                     labels = c("married", "divorced", "single")) +
    scale_y_continuous(name = "Social isolation scale") +
    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 psingleicting 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 Social isolation scale` = mean(social_isolation),
              `Calculated standard deviation of Social isolation scale` = sd(social_isolation))

kable(summary_outcome,
      format.arcgis = list(digits = 6))
Calculated average Social isolation scale Calculated standard deviation of Social isolation scale
1059.175 138.7058

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","mbta_dist"),
                 names_to = "Variable",
                 values_to = "value") |>
  group_by(Variable) |>
  summarize(`Calculated average` = mean(value)) |>
  mutate(`Input average` = case_when(Variable == "mbta_dist" ~ mbta_dist_mean,
                                     Variable == "sq_feet" ~ sq_feet_mean)) |>
  mutate(Variable = case_when(Variable == "mbta_dist" ~
                                "Distance to mbta",
                              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 mbta 4.201 4.2 -0.0009923
Square footage 1501.171 1500.0 -1.1705371

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","mbta_dist"),
                 names_to = "Variable",
                 values_to = "value") |>
  group_by(Variable) |>
  summarize(`Calculated standard deviation` = sd(value)) |>
  mutate(`Input standard deviation` = 
           case_when(Variable == "mbta_dist" ~ mbta_dist_sd,                                     Variable == "sq_feet" ~ sq_feet_sd)) |>
  mutate(Variable = case_when(Variable == "mbta_dist" ~ "Distance to mbta",
              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 mbta 0.7007 0.7 -0.0007463
Square footage 150.3681 150.0 -0.3681336

Step4.2 Regression model and the comparisons

Linear regression model summary

  model <- lm(social_isolation ~
                sq_feet +
                mbta_dist +
                married +
                divorced +
                terrace,
              data = data)
summary(model)
## 
## Call:
## lm(formula = social_isolation ~ sq_feet + mbta_dist + married + 
##     divorced + terrace, data = data)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -3.516e-11 -1.400e-13 -5.000e-14  3.000e-14  4.870e-10 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.000e+02  5.798e-13  1.725e+14   <2e-16 ***
## sq_feet      7.000e-01  3.274e-16  2.138e+15   <2e-16 ***
## mbta_dist   -5.000e+01  7.026e-14 -7.116e+14   <2e-16 ***
## married      1.000e+02  1.263e-13  7.918e+14   <2e-16 ***
## divorced     1.500e+02  1.682e-13  8.916e+14   <2e-16 ***
## terrace      2.000e+02  1.366e-13  1.464e+15   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.922e-12 on 9994 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 1.588e+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(social_isolation ~
                sq_feet +
                mbta_dist +
                married +
                divorced +
                terrace,
              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 married 100 100.0 0
Effect of divorced 150 150.0 0
Effect of terrace 200 200.0 0