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
library(tidyr)
mydata <- drop_na(mydata)
mydata$Gender <- factor(mydata$sex,
levels = c ("M", "F"),
labels = c ("Male", "Female"))
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
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())
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.
In addition to the general assumptions, some basic requirements for a linear regression model estimated by the OLS method must also be met:
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")
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.
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.
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
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
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
library(dplyr)
mydata <- mydata %>%
filter(!ID %in% c(12, 21))
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)
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
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.
H0: π2 = 0 or π½1 = π½2 = 0
H1: π2 > 0 or at least one π½π is
different from 0
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.