Project Workflow

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. Generate datasets – Compare the regression model and standard error – Figure representing the relationship between the R-squared value and the SD of regression – Is correlation = square root of the R-squared?

Extract Data from Analysis 1

library(here)
library(tidyverse)
library(knitr)
library(dplyr)

full_data <- here("generated_data.csv") |>
  read_csv()

head(full_data) |>
  kable()
sq_feet mbta_dist uniform1 uniform2 status terrace married divorced social_isolation
1478.082 4.072228 0.7897236 0.1530694 married 0 1 0 1031.0459
1518.145 4.264116 0.4494378 0.6549958 married 0 1 0 1049.4955
1474.577 4.053250 0.1807195 0.3418290 single 0 0 0 929.5417
1412.421 4.913650 0.2824777 0.8156115 married 0 1 0 943.0121
1851.967 4.215036 0.7175002 0.5467206 married 0 1 0 1285.6250
1439.535 3.948937 0.0338217 0.2574047 single 0 0 0 910.2280

1. Generate datasets

One with no error added to the outcome, and three more with varying degrees of error added

  full_data_1 = full_data
  
  full_data_2 = full_data |>
    mutate(social_isolation = social_isolation + rnorm(10000, 
          mean = 0, sd = 10))
  
  full_data_3 = full_data |>
    mutate(social_isolation = social_isolation + rnorm(10000, 
          mean = 0, sd = 50))
  
  full_data_4 = full_data |>
    mutate(social_isolation = social_isolation + rnorm(10000, 
          mean = 0, sd = 100))

2. Compare the regression model and standard error

Estimate a regression model for each of these 4 datasets (0.5pt each, 2 pts total) and confirm that the standard error of the regression is consistent with the standard deviation of the error you added to the outcome (-0.5pt deducted if missing)

  model_fullpop_1 <- lm(social_isolation ~ 
                         sq_feet +
                         mbta_dist +
                         terrace +
                         married +
                         divorced,
                       data = full_data_1)
  summary(model_fullpop_1)
## 
## Call:
## lm(formula = social_isolation ~ sq_feet + mbta_dist + terrace + 
##     married + divorced, data = full_data_1)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -8.506e-07 -2.096e-07  2.060e-09  2.064e-07  8.629e-07 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.000e+02  3.656e-08  2.735e+09   <2e-16 ***
## sq_feet      7.000e-01  2.053e-11  3.409e+10   <2e-16 ***
## mbta_dist   -5.000e+01  4.454e-09 -1.123e+10   <2e-16 ***
## terrace      2.000e+02  8.693e-09  2.301e+10   <2e-16 ***
## married      1.000e+02  7.935e-09  1.260e+10   <2e-16 ***
## divorced     1.500e+02  1.066e-08  1.407e+10   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.091e-07 on 9994 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 4.152e+20 on 5 and 9994 DF,  p-value: < 2.2e-16
  summary(model_fullpop_1)$sigma
## [1] 3.090891e-07
  r_squared_1 <- summary(model_fullpop_1)$r.squared
  if (abs(summary(model_fullpop_1)$sigma - 0) < 0.2) 
  {
    print("Assumption is True")
  } 
## [1] "Assumption is True"
  model_fullpop_2 <- lm(social_isolation ~ 
                         sq_feet +
                         mbta_dist +
                         terrace +
                         married +
                         divorced,
                       data = full_data_2)
  summary(model_fullpop_2)
## 
## Call:
## lm(formula = social_isolation ~ sq_feet + mbta_dist + terrace + 
##     married + divorced, data = full_data_2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -43.678  -6.725  -0.041   6.807  39.502 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.869e+01  1.196e+00   82.55   <2e-16 ***
## sq_feet      7.007e-01  6.714e-04 1043.62   <2e-16 ***
## mbta_dist   -4.998e+01  1.457e-01 -343.12   <2e-16 ***
## terrace      2.002e+02  2.843e-01  704.32   <2e-16 ***
## married      1.002e+02  2.595e-01  386.01   <2e-16 ***
## divorced     1.499e+02  3.487e-01  429.94   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.11 on 9994 degrees of freedom
## Multiple R-squared:  0.9949, Adjusted R-squared:  0.9949 
## F-statistic: 3.889e+05 on 5 and 9994 DF,  p-value: < 2.2e-16
  summary(model_fullpop_2)$sigma
## [1] 10.10731
  r_squared_2 <- summary(model_fullpop_2)$r.squared
  if (abs(summary(model_fullpop_2)$sigma - 10) < 0.2) 
  {
      print("Assumption is True")
  } 
## [1] "Assumption is True"
  model_fullpop_3 <- lm(social_isolation ~ 
                         sq_feet +
                         mbta_dist +
                         terrace +
                         married +
                         divorced,
                       data = full_data_3)
  summary(model_fullpop_3)
## 
## Call:
## lm(formula = social_isolation ~ sq_feet + mbta_dist + terrace + 
##     married + divorced, data = full_data_3)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -181.997  -33.947   -0.454   33.879  192.567 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 101.613298   5.931488   17.13   <2e-16 ***
## sq_feet       0.697181   0.003331  209.30   <2e-16 ***
## mbta_dist   -49.523000   0.722656  -68.53   <2e-16 ***
## terrace     198.837648   1.410420  140.98   <2e-16 ***
## married     100.924933   1.287393   78.39   <2e-16 ***
## divorced    149.898322   1.729904   86.65   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 50.15 on 9994 degrees of freedom
## Multiple R-squared:  0.8867, Adjusted R-squared:  0.8867 
## F-statistic: 1.565e+04 on 5 and 9994 DF,  p-value: < 2.2e-16
  summary(model_fullpop_3)$sigma
## [1] 50.14647
  r_squared_3 <- summary(model_fullpop_3)$r.squared
  if (abs(summary(model_fullpop_3)$sigma - 50) < 0.2) 
  {
      print("Assumption is True")
  }  
## [1] "Assumption is True"
  model_fullpop_4 <- lm(social_isolation ~ 
                         sq_feet +
                         mbta_dist +
                         terrace +
                         married +
                         divorced,
                       data = full_data_4)
  summary(model_fullpop_4)
## 
## Call:
## lm(formula = social_isolation ~ sq_feet + mbta_dist + terrace + 
##     married + divorced, data = full_data_4)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -382.68  -67.66   -0.51   66.98  365.53 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 129.369316  11.784007   10.98   <2e-16 ***
## sq_feet       0.689680   0.006618  104.22   <2e-16 ***
## mbta_dist   -53.731173   1.435692  -37.42   <2e-16 ***
## terrace     198.154465   2.802061   70.72   <2e-16 ***
## married     101.876198   2.557646   39.83   <2e-16 ***
## divorced    152.473139   3.436777   44.37   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 99.63 on 9994 degrees of freedom
## Multiple R-squared:  0.6647, Adjusted R-squared:  0.6646 
## F-statistic:  3963 on 5 and 9994 DF,  p-value: < 2.2e-16
  summary(model_fullpop_4)$sigma
## [1] 99.62531
  r_squared_4 <- summary(model_fullpop_4)$r.squared
  if (abs(summary(model_fullpop_4)$sigma - 100) < 0.2) 
  {
      print("Assumption is True")
  }  

3. Figure representing the relationship between the R-squared value and the SD of regression

Create a figure that illustrates the relationship between the R-squared value and the standard error of regression for the regressions estimated from each of your four datasets

"Calculate standard errors for each regression model"
## [1] "Calculate standard errors for each regression model"
se_1 <- summary(model_fullpop_1)$sigma
se_2 <- summary(model_fullpop_2)$sigma
se_3 <- summary(model_fullpop_3)$sigma
se_4 <- summary(model_fullpop_4)$sigma

"Gather R-squared values and their corresponding standard errors"
## [1] "Gather R-squared values and their corresponding standard errors"
r_squared_values <- c(r_squared_1, r_squared_2, r_squared_3, r_squared_4)
standard_errors <- c(se_1, se_2, se_3, se_4)

"Plot relationship between R-squared values and standard errors"
## [1] "Plot relationship between R-squared values and standard errors"
plot(r_squared_values, standard_errors,
     xlab = "R-squared values", ylab = "Standard Error of Regression",
     main = "Relationship between R-squared and Standard Error of Regression",
     pch = 16, col = "blue")

4. Is correlation = square root of the R-squared?

Select 1 of your datasets to perform the followings:

[1] Estimate a separate (univariate) regression model predicting the outcome using a single predictor, for each predictor in your full model (1pts, 0.5 partial)

  model_fullpop_1_sq_feet <- lm(social_isolation ~ 
                         sq_feet,
                       data = full_data_1)
  summary(model_fullpop_1_sq_feet)
## 
## Call:
## lm(formula = social_isolation ~ sq_feet, data = full_data_1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -234.81  -52.41  -10.46   32.56  328.54 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -9.226679   9.159194  -1.007    0.314    
## sq_feet      0.710741   0.006078 116.946   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 91.53 on 9998 degrees of freedom
## Multiple R-squared:  0.5777, Adjusted R-squared:  0.5776 
## F-statistic: 1.368e+04 on 1 and 9998 DF,  p-value: < 2.2e-16
  r_squared_1_sq_feet <- summary(model_fullpop_1_sq_feet)$r.squared

  model_fullpop_1_mbta_dist <- lm(social_isolation ~ 
                         mbta_dist,
                       data = full_data_1)
  summary(model_fullpop_1_mbta_dist)
## 
## Call:
## lm(formula = social_isolation ~ mbta_dist, data = full_data_1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -462.41  -92.51   -6.15   84.28  564.42 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1256.578      8.413   149.4   <2e-16 ***
## mbta_dist    -47.539      1.973   -24.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 136.9 on 9998 degrees of freedom
## Multiple R-squared:  0.05489,    Adjusted R-squared:  0.0548 
## F-statistic: 580.7 on 1 and 9998 DF,  p-value: < 2.2e-16
  r_squared_1_mbta_dist <- summary(model_fullpop_1_mbta_dist)$r.squared
  
  model_fullpop_1_terrace <- lm(social_isolation ~ 
                         terrace,
                       data = full_data_1)
  summary(model_fullpop_1_terrace)
## 
## Call:
## lm(formula = social_isolation ~ terrace, data = full_data_1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -428.91  -82.54    0.98   82.90  431.34 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1026.581      1.313  781.63   <2e-16 ***
## terrace      201.740      3.408   59.19   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 121.2 on 9998 degrees of freedom
## Multiple R-squared:  0.2595, Adjusted R-squared:  0.2594 
## F-statistic:  3504 on 1 and 9998 DF,  p-value: < 2.2e-16
  r_squared_1_terrace <- summary(model_fullpop_1_terrace)$r.squared
  
  model_fullpop_1_married <- lm(social_isolation ~ 
                         married,
                       data = full_data_1)
  summary(model_fullpop_1_married)
## 
## Call:
## lm(formula = social_isolation ~ married, data = full_data_1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -434.26  -97.28   -7.32   88.16  577.10 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1031.926      2.382  433.22   <2e-16 ***
## married       37.521      2.941   12.76   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 139.7 on 9998 degrees of freedom
## Multiple R-squared:  0.01602,    Adjusted R-squared:  0.01592 
## F-statistic: 162.8 on 1 and 9998 DF,  p-value: < 2.2e-16
  r_squared_1_married <- summary(model_fullpop_1_married)$r.squared  
  
  model_fullpop_1_divorced <- lm(social_isolation ~ 
                         divorced,
                       data = full_data_1)
  summary(model_fullpop_1_divorced)
## 
## Call:
## lm(formula = social_isolation ~ divorced, data = full_data_1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -447.51  -93.88   -6.93   87.27  573.36 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1045.177      1.495  698.98   <2e-16 ***
## divorced      77.561      3.907   19.85   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 138.1 on 9998 degrees of freedom
## Multiple R-squared:  0.03793,    Adjusted R-squared:  0.03783 
## F-statistic: 394.2 on 1 and 9998 DF,  p-value: < 2.2e-16
  r_squared_1_divorced <- summary(model_fullpop_1_divorced)$r.squared

[2] Calculate the correlation between each continuous predictor variable and your outcome, and confirm that it is the square root of the R-squared value from the associated univariate regression (1pts, 0.5 partial)

  cor_mat_1 <- full_data_1 |>
    select(sq_feet,
            mbta_dist,
           social_isolation) |>
    cor()
  
  check_relationship <- function(correlation, r_squared) 
  { calculated_value <- sqrt(r_squared)
    if (abs(abs(correlation) - calculated_value) < 0.001) 
      {
        return("Assumption is True")
      } 
    else 
      {
        return("Assumption is False")
      }
  }

  check_relationship(cor_mat_1["sq_feet", "social_isolation"],r_squared_1_sq_feet)
## [1] "Assumption is True"
  check_relationship(cor_mat_1["mbta_dist", "social_isolation"],r_squared_1_mbta_dist)
## [1] "Assumption is True"

[3] Confirm that the sum of the R-squared values from all four univariate regressions is close to the R-squared value for the multivariate regression (1pts, 0.5 partial)

  sum_r_squared <- r_squared_1_sq_feet + r_squared_1_mbta_dist + r_squared_1_terrace + r_squared_1_married + r_squared_1_divorced
  if (abs(sum_r_squared - r_squared_1) < 0.1) 
    {
      print("Assumption is True")
    } 
## [1] "Assumption is True"