More Regression

Check-In : Interpreting A Multivariate Regression Table

Note : number of friends = facebook friends

Interpreting the Regression Table

  • Model 1 :

  • Model 2 :

  • Model 3 :

  • Model 4 :

From Regression Table to Results Section

Multiple Regression Recap : Hair Length Predicts Height?!?!?

  1. ICE-BREAKER :

    • let’s keep it light mode : if you HAD to get a tattoo, what would you get? where would you get it? would it face toward you or other people?

    • bring on the heavy mode : if you could change one thing about your childhood, what would you change?

  2. MODELS : What Do You Observe in Model 1? Model 2? Model 3?

  3. Other Questions That You, the Students, Have?

## Data Cleaning
library(gplots)
d <- read.csv("~/Dropbox/!WHY STATS/Class Datasets/101 - Class Datasets/Mini Data/mini_DATA.csv", stringsAsFactors = T)

d$height[d$height < 10 | d$height > 100] <- NA
levels(d$is.female)[1] <- NA
levels(d$long.hair)[1] <- NA

par(mfrow = c(1,3))
hist(d$height)
plot(d$is.female, xlab = "Is Female?")
plot(d$long.hair, xlab = "Has Long Hair?")


Call:
lm(formula = height ~ long.hair, data = d)

Residuals:
     Min       1Q   Median       3Q      Max 
-11.8715  -1.8715   0.1285   2.9355   8.1285 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   67.0645     0.6603 101.561  < 2e-16 ***
long.hairYes  -3.1930     0.7898  -4.043 0.000103 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.677 on 101 degrees of freedom
  (5 observations deleted due to missingness)
Multiple R-squared:  0.1393,    Adjusted R-squared:  0.1308 
F-statistic: 16.34 on 1 and 101 DF,  p-value: 0.0001033


Call:
lm(formula = height ~ is.female, data = d)

Residuals:
     Min       1Q   Median       3Q      Max 
-11.6506  -1.6506   0.3494   1.9147   8.3494 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   68.5200     0.6711 102.097  < 2e-16 ***
is.femaleYes  -4.8694     0.7712  -6.314 7.35e-09 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.356 on 101 degrees of freedom
  (5 observations deleted due to missingness)
Multiple R-squared:  0.283, Adjusted R-squared:  0.2759 
F-statistic: 39.87 on 1 and 101 DF,  p-value: 7.351e-09


Call:
lm(formula = height ~ long.hair + is.female, data = d)

Residuals:
     Min       1Q   Median       3Q      Max 
-11.5621  -1.5621   0.3544   2.1683   7.8101 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   68.6456     0.6975  98.421  < 2e-16 ***
long.hairYes  -0.6278     0.9174  -0.684    0.495    
is.femaleYes  -4.4557     0.9815  -4.539 1.58e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.364 on 100 degrees of freedom
  (5 observations deleted due to missingness)
Multiple R-squared:  0.2863,    Adjusted R-squared:  0.2721 
F-statistic: 20.06 on 2 and 100 DF,  p-value: 4.725e-08

Multiple Regression : Visualized in Multi-Dimensional Space!

The code below may not work on your computer; see lecture recording for an interpretation / explanation!

NOTE : I used AI (Gemini) to generate the code for this graph! Here was my prompt : “make an interactive 3d plot in R from the dataset d using the numeric outcome variable named height and two categorical predictor variables named long.hair and is.female. Generate a plane from the linear model to fill this 3D plot.”


d <- read.csv("~/Dropbox/!WHY STATS/Class Datasets/101 - Class Datasets/Mini Data/mini_DATA.csv", stringsAsFactors = T)
library(plotly) # you will need to install this.
d$height[d$height < 10 | d$height > 100] <- NA
levels(d$is.female)[1] <- NA
levels(d$long.hair)[1] <- NA

# PROF used AI (gemini) to generate this code.
##### I used to use the function scatter3d from the "car" library 
##### ....but it stopped working.

# Create the interactive 3D scatter plot
model <- lm(height ~ long.hair + is.female, data = d)
plot_ly(data = d, 
        x = ~long.hair, 
        y = ~is.female, 
        z = ~height, 
        type = "scatter3d", 
        mode = "markers",
        marker = list(size = 5, opacity = 0.8)) %>%
  layout(scene = list(xaxis = list(title = 'Hair Length'),
                      yaxis = list(title = 'Is Female'),
                      zaxis = list(title = 'Height')))

hair_levels <- levels(d$long.hair)
levels(hair_levels)
gender_levels <- levels(d$is.female)

# Create a grid for all combinations
grid <- expand.grid(long.hair = hair_levels, 
                    is.female = gender_levels)

# 3. Predict height across the grid and format as a matrix
z_matrix <- matrix(predict(model, newdata = grid), 
                   nrow = length(hair_levels), 
                   ncol = length(gender_levels))

# 4. Create the plot
plot_ly(d, x = ~long.hair, y = ~is.female, z = ~height) %>%
  add_markers(marker = list(size = 3, opacity = 0.6)) %>%
  add_surface(x = hair_levels, 
              y = gender_levels, 
              z = t(z_matrix), # Transpose to align with axes
              opacity = 0.7, 
              showscale = FALSE) %>%
  layout(scene = list(xaxis = list(title = 'Hair Length'),
                      yaxis = list(title = 'Is Female'),
                      zaxis = list(title = 'Height')))

Reporting Effects in a Regression Table.

Table 1. Unstandardized Regression Coefficients; Predicting Height from Long.Hair and Is.Female.

Model 1 Model 2 Model 3
Intercept
Long.Hair (0 = No; 1 = Yes)
Is.Female (0 = No; 1 = Yes)
\(R^2\)

There’s a Package in R For This!

Table 1. Unstandardized Regression Coefficients; Predicting Height from Long.Hair and Is.Female.

Model 1Model 2Model 3
Long Hair (0 = No, 1 = Yes)-3.19 ***       -0.63    
(0.79)          (0.92)   
Is Female (0 = No, 1 = Yes)       -4.87 ***-4.46 ***
       (0.77)   (0.98)   
N103       103       103       
R20.14    0.28    0.29    
*** p < 0.001; ** p < 0.01; * p < 0.05.
install.packages("jtools") # a new package!!!
library(jtools) # make sure you installed the new package first.
export_summs(moda, modb, modc,
             coefs = c("Long Hair (0 = No, 1 = Yes)" = "long.hairYes",
                       "Is Female (0 = No, 1 = Yes)" = "is.femaleYes"))

Optional : Chapter 12 (on Interaction Effects)

  • Key Idea : the effect of the IV on the DV depends on another IV.

  • Example : Average Time to Solve Puzzle ~ Sex * Culture + Error

Final Project Workshop

  • Reviewing Regression Tables —> Writing Your Results

  • Reviewing Introduction Outlines

  • Reviewing Methods