Research question: How height and sex affect weight of the person?


Data

data()
data(package = .packages(all.available=TRUE))
#install.packages("CarData")
library(carData)
mydata <- force(Davis)

head(mydata)
##   sex weight height repwt repht
## 1   M     77    182    77   180
## 2   F     58    161    51   159
## 3   F     53    161    54   158
## 4   M     68    177    70   175
## 5   F     59    157    59   155
## 6   M     76    170    76   165


Data explanation

  • Unit of observation: Subject (Men and women engaged in regular exercise)
  • Sample size: 200
  • Definition of all variables:
    • sex: A factor with levels: F-female and M-male.
    • weight: Measured weight in kg of subject.
    • height: Measured height in cm of subject.
    • repwt: Reported weight in kg of subject.
    • repht: Reported height in cm of subject.
  • Source: Personal communication from C. Davis, Departments of Physical Education and Psychology, York University.


Data manipulation and descriptive statistics

  • Cleaning out missing values
library(tidyr)
mydata <- drop_na(mydata)
  • Converting categorical variable to factors
mydata$Gender <- factor(mydata$sex,
                       levels = c ("M", "F"),
                       labels = c ("Male", "Female"))
  • Statistical description
library(pastecs)
## 
## Attaching package: 'pastecs'
## The following object is masked from 'package:tidyr':
## 
##     extract
round(stat.desc(mydata[ ,-c(1, 4, 5, 6)]), 2)
##                weight   height
## nbr.val        181.00   181.00
## nbr.null         0.00     0.00
## nbr.na           0.00     0.00
## min             39.00    57.00
## max            166.00   197.00
## range          127.00   140.00
## sum          12001.00 30798.00
## median          63.00   169.00
## mean            66.30   170.15
## SE.mean          1.14     0.92
## CI.mean.0.95     2.25     1.81
## var            235.35   151.59
## std.dev         15.34    12.31
## coef.var         0.23     0.07


  • Mean for weight: The mean weight of 66.3 is the average weight calculated from all the weights in the dataset.
  • Median for height: The median height of 169.0 indicates that half of the heights in the dataset are up to 169.0 cm, and half are above 169.0 cm.
  • Standard deviation for height: Standard deviation of 12.31 means that, on average, the individual values in the dataset deviate from the mean by around 12.31 units.


  • Creating variable ID for filtering data later
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:pastecs':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mydata <- mydata %>%
  mutate(ID = row_number())


Multiple regression model

As my research question was β€œHow height and sex affect weight of the person?”, dependent variable is weight while explanatory variables are height and gender. I would expect that as person is higher, the weight increases and that if a person is male, weight is higher than if it is a female. That being said, I would expect positive coefficient of variable height and negative coefficient for variable gender since males in this dummy variable are denoted by 0 and females by 1.


Regression analysis

Assumptions
  • Linearity in parameters
  • The expected value of the errors equals 0
  • Homoskedasticity π‘‰π‘Žπ‘Ÿ (πœ€|𝒙) = 𝜎2
  • Normal distribution of errors
  • Errors are independent, Cov πœ€π‘–, πœ€π‘— = 0 for each 𝑖 β‰  𝑗, this assumption is fulfilled.
  • No perfect multicolinearity
  • The number of units is greater than the number of estimated parameters: 𝑛 > π‘˜, this assumption is fulfilled.


In addition to the general assumptions, some basic requirements for a linear regression model estimated by the OLS method must also be met:

Requirements
  1. The dependent variable is numerical, the explanatory variables can be numerical or dichotomous (i.e., Dummy variable). This requirement is fulfilled.
  2. Each explanatory variable must vary, but it is desirable that the explanatory variables assume as wide a range of possible values as possible. This requirement is fulfilled..
  3. Potential outliers and units that have a large impact on the estimated regression function are removed from the data.
  4. There is not too strong multicolinearity.


Checking for linearity


library(car)
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
scatterplot(mydata$height, mydata$weight, 
            smooth = FALSE,
            boxplot = FALSE,
            ylim = c(0, 140), 
            xlim = c(140, 200),
            main = "Relationship between height and weight",
            xlab = "Height", 
            ylab = "Weight")

  • From the graph we can say that relationship between height and weight is linear which means that first assumption is met.


Checking for multicolinearity with variance inflation factor (VIF)
fit <- lm(weight ~ height + Gender ,
           data = mydata)

vif(fit)
##   height   Gender 
## 1.513381 1.513381

Since vif is the same for both I will not calculate the mean. This is only due to the fact that there are only two explanatory variables.

We can conclude that we do not have a problem wiht multicolinearity nor too strong multicolinearity since vif is less than 5.


I’ll now assess the normal distribution of errors by examining the histogram of standardized residuals and complete it with Shapiro wilk normality test.
mydata$stdResid <- round(rstandard(fit), 3)
mydata$CooksD <- round(cooks.distance(fit), 3) 

hist (mydata$stdResid,
      xlab = "Standardized residuals",
      ylab = "Frequency",
      main = "Histogram of standardized residuals")

shapiro.test(mydata$stdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata$stdResid
## W = 0.77428, p-value = 2.045e-15

H0: Errors are normally distributed.
H1: Errors are not normally distributed.

Since p-value<0.001 we can reject the null hypothesis and thus say that errors are not normally distributed. This would normally be an issue however my sample size is 181 which is big enough this is not an issue.

Also with the standardized residulas the presence of outliers can be checked. Outliers are evident, as certain values exceed 3. Those outliers should be removed from the data.


Additionally I will check the values of the cooks distances to check for the units of high imapct.
hist(mydata$CooksD,
     xlab = "Cooks Distances",
     ylab = "Frequency",
     main = "Histogram of Cooks distances")

head(mydata[order(mydata$stdResid), c("ID","stdResid")],5) 
##      ID stdResid
## 165 152   -2.093
## 156 145   -2.080
## 82   77   -2.027
## 29   29   -1.787
## 60   56   -1.715
  • As expected no values of standardized residuals are less than -3.
head(mydata[order(-mydata$stdResid), c("ID", "stdResid")], 5)
##    ID stdResid
## 12 12    9.805
## 21 21    3.622
## 97 92    2.440
## 54 52    2.357
## 30 30    2.216
  • Outliers are ID 12 and ID 21 and I will remove them.
head(mydata[order(-mydata$CooksD), c("ID", "CooksD")], 10)
##      ID CooksD
## 12   12 57.196
## 21   21  0.055
## 165 152  0.037
## 97   92  0.030
## 169 156  0.030
## 54   52  0.028
## 65   61  0.027
## 82   77  0.025
## 156 145  0.025
## 30   30  0.023
  • The unit of high impact that I will remove is with ID 12. Additionally the unit with ID 21 makes another gap, however it is very very small. Anyway due to standardized residuals both units will be removed.
library(dplyr)
mydata <- mydata %>%
  filter(!ID %in% c(12, 21))
Checking for homoskedasticity
fit <- lm (weight ~ height + Gender, 
           data = mydata)

mydata$stdFitted <- scale(fit$fitted.values)

library(car)
scatterplot(stdResid ~ stdFitted,
            ylab = "Standardized residuals",
            xlab = "Standardized fitted values",
            smooth = FALSE,
            regLine = FALSE,
            boxplots = FALSE,
            data=mydata)

  • Breusch Pagan test for checking homoskedasticity.
library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_test_breusch_pagan(fit)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                Data                
##  ----------------------------------
##  Response : weight 
##  Variables: fitted values of weight 
## 
##          Test Summary           
##  -------------------------------
##  DF            =    1 
##  Chi2          =    21.47834 
##  Prob > Chi2   =    3.578479e-06

H0: The variance is constant.

H1: The variance is not constant.

We reject the null hypothesis at (p <0.001) and conclude that homoscedasticity assumption is violated. Now I will have to use Robust standard errors.

library(estimatr)
## Warning: package 'estimatr' was built under R version 4.3.2
fit_robust <- lm_robust(weight ~ height + Gender,
                        se_type = "HC1",
                        data = mydata)
summary(fit_robust)
## 
## Call:
## lm_robust(formula = weight ~ height + Gender, data = mydata, 
##     se_type = "HC1")
## 
## Standard error type:  HC1 
## 
## Coefficients:
##              Estimate Std. Error t value  Pr(>|t|)  CI Lower CI Upper
## (Intercept)  -70.7476     18.618  -3.800 1.991e-04 -107.4899  -34.005
## height         0.8214      0.106   7.746 7.205e-13    0.6121    1.031
## GenderFemale  -7.3651      1.650  -4.464 1.432e-05  -10.6211   -4.109
##               DF
## (Intercept)  176
## height       176
## GenderFemale 176
## 
## Multiple R-squared:  0.6498 ,    Adjusted R-squared:  0.6458 
## F-statistic: 128.2 on 2 and 176 DF,  p-value: < 2.2e-16


Explanation of coefficients:

We can reject null hypothesis(H0: 𝛽𝑗 = 0, H1: 𝛽𝑗 β‰  0), (p<0.001), which means that the both explanatory variables affect the dependent variable, and I will now explain them.


  • height: If height increases by 1 cm, the weight of the subject will increase by 0.82 kg on average (p<0.001), controlling for gender. The sign of the coefficient was expected.
  • GenderFemale: Given the height of the subject, females are on average 7.37 kg lighter than males. (p<0.001) The sign of coefficient was expected.


  • Coefficient of determination: 𝑅2 = 64.88% of variability of weight is explained by linear effect of all explanatory variables.


  • F-statistics:

H0: 𝜌2 = 0 or 𝛽1 = 𝛽2 = 0
H1: 𝜌2 > 0 or at least one 𝛽𝑗 is different from 0

  • We reject the null hypothesis at p-value less than 0.001 and based on this conclude that at least some of the explanatory variables have an impact on weight.


Coefficient of multiple correlation
sqrt(summary(fit)$r.squared)
## [1] 0.8061179

This is the square root of coefficient of determination and it shows us the relationship between all variables in the model.The relationship between the weight, height and gender variables is strong.