library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
mydata <- read_delim("C:\\Users\\abhir\\OneDrive\\Documents\\luddy\\H510-Stats,R\\adult.csv", delim = ",")
## Rows: 32561 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Work-Class, Education, Marital Status, Occupation, Relationship, Ra...
## dbl (6): Age, Fnl_Wgt, Education_Level, Capital_Gain, Capital_Loss, hrs_week
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mydata
## # A tibble: 32,561 × 15
## Age `Work-Class` Fnl_Wgt Education Education_Level `Marital Status`
## <dbl> <chr> <dbl> <chr> <dbl> <chr>
## 1 39 " State-gov" 77516 " Bachelo… 13 " Never-married"
## 2 50 " Self-emp-not-inc" 83311 " Bachelo… 13 " Married-civ-s…
## 3 38 " Private" 215646 " HS-grad" 9 " Divorced"
## 4 53 " Private" 234721 " 11th" 7 " Married-civ-s…
## 5 28 " Private" 338409 " Bachelo… 13 " Married-civ-s…
## 6 37 " Private" 284582 " Masters" 14 " Married-civ-s…
## 7 49 " Private" 160187 " 9th" 5 " Married-spous…
## 8 52 " Self-emp-not-inc" 209642 " HS-grad" 9 " Married-civ-s…
## 9 31 " Private" 45781 " Masters" 14 " Never-married"
## 10 42 " Private" 159449 " Bachelo… 13 " Married-civ-s…
## # ℹ 32,551 more rows
## # ℹ 9 more variables: Occupation <chr>, Relationship <chr>, Race <chr>,
## # Sex <chr>, Capital_Gain <dbl>, Capital_Loss <dbl>, hrs_week <dbl>,
## # Country <chr>, Annual_pay <chr>
model <- aov(hrs_week ~ Occupation + Age + Education + Sex + Occupation:Sex, data = mydata)
anova_result <- anova(model)
print(anova_result)
## Analysis of Variance Table
##
## Response: hrs_week
## Df Sum Sq Mean Sq F value Pr(>F)
## Occupation 14 481500 34393 263.850 < 2.2e-16 ***
## Age 1 6522 6522 50.038 1.538e-12 ***
## Education 15 102422 6828 52.383 < 2.2e-16 ***
## Sex 1 102942 102942 789.732 < 2.2e-16 ***
## Occupation:Sex 13 32221 2479 19.014 < 2.2e-16 ***
## Residuals 32516 4238458 130
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Reason for chosing these variables Age:Inclusion rationale: Age could potentially influence the number of hours worked per week. Younger individuals might be more likely to work longer hours due to entry-level positions or career ambitions, while older individuals might prefer more balanced work-life arrangements. Multicollinearity concerns: Age might be correlated with other variables such as experience or seniority, which could lead to multicollinearity issues. However, if it adds significant explanatory power beyond other variables, it can still be included. Education:
Inclusion rationale: Education level could affect the type of occupations individuals are qualified for and subsequently their hours worked. For example, individuals with higher education levels might work longer hours in certain professions or industries. Multicollinearity concerns: Education level might also be correlated with occupation or income, potentially leading to multicollinearity. However, it provides valuable information about the individual’s qualifications and socioeconomic status. Gender:
Inclusion rationale: Gender may influence both occupation choice and hours worked per week due to societal norms, discrimination, or differences in family responsibilities. Including gender allows for examining potential gender disparities in hours worked. Multicollinearity concerns: Gender is a categorical variable, so multicollinearity with other continuous variables might not be a significant issue. However, it’s important to consider potential interactions with other variables, such as occupation, to avoid spurious relationships. Interaction term (Occupation:Gender):
Inclusion rationale: This interaction term allows for investigating whether the effect of occupation on hours worked varies depending on gender. It captures potential differences in occupational choices or work expectations between genders. Multicollinearity concerns: Interaction terms can exacerbate multicollinearity issues if the constituent variables are highly correlated. It’s essential to assess the correlation between Occupation and Gender and ensure that the interaction term adds meaningful explanatory power to the model.
# Fit the model
model <- lm(hrs_week ~ Occupation + Age + Education + Sex + Occupation:Sex, data = mydata)
# Load required library for diagnostic plots
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
# Diagnostic plots
par(mfrow=c(2,2)) # Set layout for multiple plots
# Residuals vs Fitted Values Plot
plot(model, 1)
#Residuals vs Fitted Values Plot:
Indications of Issues: If the residuals show a non-linear pattern or if the spread of residuals increases or decreases as fitted values change, it indicates potential issues with the model, such as non-linearity or heteroscedasticity. Support for Assumption: If the residuals are randomly scattered around the horizontal line at zero without any discernible pattern, it suggests that the linear relationship assumption is met. The absence of a pattern indicates that the model captures the relationship between the predictor variables and the response variable adequately.
# Normal Q-Q Plot
plot(model, 2)
#Normal Q-Q Plot:
Indications of Issues: If the points in the Q-Q plot deviate substantially from the diagonal line, especially at the tails of the distribution, it suggests that the residuals are not normally distributed. Departures from the diagonal line indicate departures from normality. Support for Assumption: If the points closely follow the diagonal line, it suggests that the residuals are approximately normally distributed. This supports the assumption of normality for the linear regression model.
# Scale-Location Plot
plot(model, 3)
#Scale Location Indications of Issues: Look for patterns or trends in
the spread of residuals as fitted values change. If the spread of
residuals increases or decreases systematically with fitted values, it
indicates heteroscedasticity. Support for Assumption: If the points are
randomly scattered around a horizontal line without any obvious pattern,
it suggests that the assumption of homoscedasticity is met. A consistent
spread of residuals across the range of fitted values supports the
assumption that the variance of the residuals is constant.
# Residuals vs Leverage Plot
plot(model, 5)
#Residuals vs Leverage Plot:
Indications of Issues: Identify observations with high leverage (values that are outliers in terms of their predictor variables) and high standardized residuals. Observations in the upper right or lower right quadrants are of particular interest as they have both high leverage and high residuals and may be influential points. Support for Assumption: Look for observations that fall within the acceptable range of leverage and residuals. Most observations should fall within the dashed lines indicating Cook’s distance. The plot helps to identify influential observations that may disproportionately affect the regression coefficients.
# Reset layout
par(mfrow=c(1,1))