options(width=70)
data()
data(package = .packages(all.available=TRUE))
library(carData)
mydata <- force(Mroz)
head(mydata)
## lfp k5 k618 age wc hc lwg inc
## 1 yes 1 0 32 no no 1.2101647 10.910
## 2 yes 0 2 30 no no 0.3285041 19.500
## 3 yes 1 3 35 no no 1.5141279 12.040
## 4 yes 0 3 34 no no 0.0921151 6.800
## 5 yes 1 2 31 yes no 1.5242802 20.100
## 6 yes 0 0 54 no no 1.5564855 9.859
Unit of observation: One married women
Sample size: 753
Definition of all variables:
fp: labor-force participation; a factor with levels: no; yes.
k5: number of children 5 years old or younger.
k618: number of children 6 to 18 years old.
age: in years.
wc: wife’s college attendance; a factor with levels: no; yes.
hc: husband’s college attendance; a factor with levels: no; yes.
lwg: log expected wage rate; for women in the labor force, the actual wage rate; for women not in the labor force, an imputed value based on the regression of lwg on the other variables.
inc: family income exclusive of wife’s income.
Source: Mroz, T. A. (1987) The sensitivity of an empirical model of married women’s hours of work to economic and statistical assumptions. Econometrica 55, 765–799.
I wont use first variable since the only value shown is “yes”.
mydata$wcF <- factor(mydata$wc,
levels = c ("no", "yes"),
labels = c ("no", "yes"))
mydata$hcF <- factor(mydata$hc,
levels = c ("no", "yes"),
labels = c ("no", "yes"))
summary(mydata)
## lfp k5 k618 age wc
## no :325 Min. :0.0000 Min. :0.000 Min. :30.00 no :541
## yes:428 1st Qu.:0.0000 1st Qu.:0.000 1st Qu.:36.00 yes:212
## Median :0.0000 Median :1.000 Median :43.00
## Mean :0.2377 Mean :1.353 Mean :42.54
## 3rd Qu.:0.0000 3rd Qu.:2.000 3rd Qu.:49.00
## Max. :3.0000 Max. :8.000 Max. :60.00
## hc lwg inc wcF hcF
## no :458 Min. :-2.0541 Min. :-0.029 no :541 no :458
## yes:295 1st Qu.: 0.8181 1st Qu.:13.025 yes:212 yes:295
## Median : 1.0684 Median :17.700
## Mean : 1.0971 Mean :20.129
## 3rd Qu.: 1.3997 3rd Qu.:24.466
## Max. : 3.2189 Max. :96.000
library (psych)
psych:: describe(mydata[ , c("age", "inc")])
## vars n mean sd median trimmed mad min max range skew
## age 1 753 42.54 8.07 43.0 42.32 10.38 30.00 60 30.00 0.15
## inc 2 753 20.13 11.63 17.7 18.62 8.24 -0.03 96 96.03 2.21
## kurtosis se
## age -1.02 0.29
## inc 8.35 0.42
summary(mydata[, c("wcF","hcF")])
## wcF hcF
## no :541 no :458
## yes:212 yes:295
Among the observed cases, 212 wives have attended college (“yes”), while 541 wives have not attended college (“no”), illustrating the count or frequency of each category and giving a sense of the prevalence of wife’s college attendance in the dataset.
Among the observed cases, 295 husbands have attended college (“yes”), while 458 husbands have not attended college (“no”), illustrating the count or frequency of each category and providing insights into the prevalence of husband’s college attendance in the dataset.
To compute the Pearson correlation coefficient for a correlation
analysis, it’s essential to ensure that certain assumptions are met
beforehand.
Reducing the size of the sample
set.seed(1)
mydatasample <- mydata[sample(nrow(mydata), 250), ]
head(mydata)
## lfp k5 k618 age wc hc lwg inc wcF hcF
## 1 yes 1 0 32 no no 1.2101647 10.910 no no
## 2 yes 0 2 30 no no 0.3285041 19.500 no no
## 3 yes 1 3 35 no no 1.5141279 12.040 no no
## 4 yes 0 3 34 no no 0.0921151 6.800 no no
## 5 yes 1 2 31 yes no 1.5242802 20.100 yes no
## 6 yes 0 0 54 no no 1.5564855 9.859 no no
library(car)
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
scatterplotMatrix(mydatasample[ , c(-1, -2, -3, -5, -6, -7, -9, -10)], smooth= FALSE)
cor(mydatasample$age, mydatasample$inc,
method = "pearson",
use = "complete.obs")
## [1] 0.1300861
cor.test(mydatasample$age, mydatasample$inc,
method = "pearson",
use ="complete.obs")
##
## Pearson's product-moment correlation
##
## data: mydatasample$age and mydatasample$inc
## t = 2.0662, df = 248, p-value = 0.03985
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.006117819 0.250116365
## sample estimates:
## cor
## 0.1300861
H0: ρ = 0
H1: ρ =/=0
The typical correlation coefficient is not suitable for analyzing the association between two categorical variables. Instead, it is more appropriate to analyze frequencies.
However, to conduct a valid Pearson Chi-square test, certain assumptions must be met.
H0: There is no association between two categorical variables
wife’s college attendance and husband’s college attendance.
H1: There is association between two categorical variables wife’s college attendance and husband’s college attendance.
results <- chisq.test (mydata$wcF, mydata$hcF,
correct = TRUE) #Yates' correction 2x2 table
results
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mydata$wcF and mydata$hcF
## X-squared = 210.69, df = 1, p-value < 2.2e-16
Observed (empirical) frequency:
addmargins (results$observed)
## mydata$hcF
## mydata$wcF no yes Sum
## no 417 124 541
## yes 41 171 212
## Sum 458 295 753
Expected (theoretical) frequency:
round(results$expected, 2)
## mydata$hcF
## mydata$wcF no yes
## no 329.05 211.95
## yes 128.95 83.05
329.05 - If there is no association between two categorical variables we would expect to see 329.05 would see that married women did not attend college and have husbands who did not attend collage as well.
Since all expected frequencies are greater than 5, second assumption is met.
Since all expected frequencies are greater than 5, third assumption is also met.
Standardized residuals help determine if the differences between what actually happened and what was expected are statistically important.
round(results$res, 2)
## mydata$hcF
## mydata$wcF no yes
## no 4.85 -6.04
## yes -7.74 9.65
4.85: The actual number of married women who did not attend college and have husband who did not attend college as well is higher than expected. (Alfa= 0.1%)
Pearson’s Chi-squared test: Based on the sample data, we can reject the null hypothesis (p<0.001) and conclude that there is association between wife’s college attendance and husband’s college attendance.
addmargins(round(prop.table(results$observed), 3))
## mydata$hcF
## mydata$wcF no yes Sum
## no 0.554 0.165 0.719
## yes 0.054 0.227 0.281
## Sum 0.608 0.392 1.000
addmargins(round(prop.table(results$observed, 1), 3), 2)
## mydata$hcF
## mydata$wcF no yes Sum
## no 0.771 0.229 1.000
## yes 0.193 0.807 1.000
addmargins(round(prop.table(results$observed, 2), 3), 1)
## mydata$hcF
## mydata$wcF no yes
## no 0.91 0.42
## yes 0.09 0.58
## Sum 1.00 1.00
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
effectsize::cramers_v(mydata$wcF, mydata$hcF)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.53 | [0.47, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.53)
## [1] "very large"
## (Rules: funder2019)
oddsratio(mydata$wcF, mydata$hcF)
## Odds ratio | 95% CI
## --------------------------
## 14.03 | [9.44, 20.83]
interpret_oddsratio(14.03)
## [1] "large"
## (Rules: chen2010)