0
votes

I ran a linear regression

lm.fit <- lm(intp.trust~age+v225+age*v225+v240+v241+v242,data=intp.trust)

summary(lm.fit)

and get the following results

Call:
lm(formula = intp.trust ~ age + v225 + age * v225 + v240 + v241 + 
    v242, data = intp.trust)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.32050 -0.33299 -0.04437  0.30899  2.35520 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  2.461e+00  2.881e-02  85.418  < 2e-16 ***
age         -2.416e-03  5.144e-04  -4.697 2.66e-06 ***
v225         5.794e-04  1.574e-02   0.037    0.971    
v240         2.111e-02  2.729e-03   7.734 1.07e-14 ***
v241        -1.177e-03  1.958e-04  -6.014 1.83e-09 ***
v242        -1.473e-02  4.166e-04 -35.354  < 2e-16 ***
age:v225     4.214e-06  3.101e-04   0.014    0.989    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.4833 on 34845 degrees of freedom
  (21516 observations deleted due to missingness)
Multiple R-squared:  0.05789,   Adjusted R-squared:  0.05773 
F-statistic: 356.8 on 6 and 34845 DF,  p-value: < 2.2e-16

"consider the residuals from the regression above. compare the residual distributions for females and males using an appropriate graph?" Males and females is coded using variable v225. How do I go about on creating this graph? at first I created :

lm.res <- resid(lm.fit)

but I'm not sure what the next step is. The graph is supposed to be a scatterplot of residuals with different colour for females and males.

I tried this but was not working

ggplot(intp.trust, aes(x = intp.trust, y = lm.res, color = v225)) + geom_point()
2
Could you provide more details on the issue that you are encountering, please? E.g., are you getting an error message or an empty plot without any errors? I think your ggplot() can work, but one possible reason it might not is that you may have missing values in intp.trust$intp.trust or intp.trust$v225. - LC-datascientist

2 Answers

0
votes

In this line:

ggplot(intp.trust, aes(x = intp.trust, y = lm.res, color = v225)) + geom_point()

You are saying: "go look in the data.frame intp.trust for a variable called lm.res, and plot that as y"

But you created lm.res as standalone object, not as a column of intp.trust. Assign the residuals from your model to a new column in the data.frame like this:

intp.trust$lm.res <- resid(lm.fit)

And it should work. Example with dummy data:

library(ggplot2)

# generate data
true_function <- function(x, is_female) {
  ifelse(is_female, 5, 2) +
    ifelse(is_female, -1.5, 1.5) * x +
    rnorm(length(x))
}

set.seed(123)
dat <- data.frame(x = runif(200, 1, 5), is_female = rbinom(200, 1, .5))
dat$y <- with(dat, true_function(x, is_female))

# regression
lm_fit <- lm(y ~ x + as.factor(is_female), data=dat)
# add residuals to data.frame
dat$resid <- resid(lm_fit)

# plot
ggplot(dat, aes(x=x, y=resid, color=as.factor(is_female))) +
  geom_point()

enter image description here

0
votes

Here is a sample that you could follow and get what you want

# Sample Data
x_1 <- rnorm(100)
x_2 <- runif(100, 10, 30)
x_3 <- rnorm(100) * runif(100)
y <- rnorm(100, mean = 10)
gender <- sample(c("F", "M"), replace = TRUE)
df <- data.frame(x_1, x_2, x_3, y, gender)
# Fit model
lm.fit <- lm(y ~ x_1 + x_2 + x_1 * x_2 + x_3, data = df)
# Update data.frame
df$residuals <- lm.fit$residuals
# Scatter Residuals
ggplot(df) +
  geom_point(aes(x = as.numeric(row.names(df)), y = residuals, color = gender)) +
  labs(x = 'Index', y = 'Residual value', title = 'Residual scatter plot')