Author: Edi Oblak
mydata1 <- force(Wong)
head(mydata1)
## id days duration sex age piq viq
## 1 3358 30 4 Male 20.67077 87 89
## 2 3535 16 17 Male 55.28816 95 77
## 3 3547 40 1 Male 55.91513 95 116
## 4 3592 13 10 Male 61.66461 59 73
## 5 3728 19 6 Male 30.12731 67 73
## 6 3790 13 3 Male 57.06229 76 69
Unit of observation in our data is patient, sample size is 331, which is appropriate for our analysis. It is good to know that there were 200 patients tested, some of them twice. Those who were tested twice were not tested at the same time, but in a relevant time difference.
Source of data: Wong, P. P., Monette, G., and Weiner, N. I. (2001) Mathematical models of cognitive recovery. Brain Injury, 15, 519–530.
summary(mydata1[ , -1]) # We don't want to describe id.
## days duration sex age piq viq
## Min. : 13.0 Min. : 0.0 Female: 71 Min. : 6.513 Min. : 50.00 Min. : 64.00
## 1st Qu.: 59.0 1st Qu.: 1.0 Male :260 1st Qu.:21.737 1st Qu.: 77.00 1st Qu.: 85.00
## Median : 150.0 Median : 7.0 Median :26.877 Median : 87.00 Median : 94.00
## Mean : 481.9 Mean : 14.3 Mean :31.853 Mean : 87.56 Mean : 94.96
## 3rd Qu.: 416.0 3rd Qu.: 16.0 3rd Qu.:40.923 3rd Qu.: 97.00 3rd Qu.:105.00
## Max. :11628.0 Max. :255.0 Max. :80.033 Max. :133.00 Max. :132.00
RQ1: Is there a correlation between the scores of patients achieved in performance IQ and verbal IQ?
We can perform some graphical analysis.
library(car)
scatterplotMatrix(mydata1[ , c(-1,-2,-3,-4,-5)], smooth=FALSE)
### Pearson correlation coefficient
rcorr(as.matrix(mydata1[ , c(-1,-2,-3,-4,-5)]),
type = "pearson") # Numerical data!
## piq viq
## piq 1.00 0.65
## viq 0.65 1.00
##
## n= 331
##
##
## P
## piq viq
## piq 0
## viq 0
H0: Population correlation coefficient between piq and viq is 0. H1: Population correlation coefficient between piq and viq is not 0.
We reject H0 (p < 0.001). There is positive linear correlation between score on performance IQ and verbal IQ of patients after being in coma for several days due to severe injuries of brains. Linear relationship is positive and semi-strong (|r| = 0.65).
mydata2 <- force(Chile)
head(mydata2)
## region population sex age education income statusquo vote
## 1 N 175000 M 65 P 35000 1.00820 Y
## 2 N 175000 M 29 PS 7500 -1.29617 N
## 3 N 175000 F 38 P 15000 1.23072 Y
## 4 N 175000 F 49 P 35000 -1.03163 N
## 5 N 175000 F 23 S 35000 -1.10496 N
## 6 N 175000 F 28 P 7500 -1.04685 N
Unit of observation in our data is respondent on national survey conducted in April and May of 1988 by FLASCO/Chile. Sample size is 2700, which is appropriate for our analysis. The only problem of data is that there are some missing data, luckily already marked with NA. All the data that should be in factor format is already in the right form.
Source of data: Personal communication from FLACSO/Chile.
summary(mydata2)
## region population sex age education income statusquo vote
## C :600 Min. : 3750 F:1379 Min. :18.00 P :1107 Min. : 2500 Min. :-1.80301 A :187
## M :100 1st Qu.: 25000 M:1321 1st Qu.:26.00 PS : 462 1st Qu.: 7500 1st Qu.:-1.00223 N :889
## N :322 Median :175000 Median :36.00 S :1120 Median : 15000 Median :-0.04558 U :588
## S :718 Mean :152222 Mean :38.55 NA's: 11 Mean : 33876 Mean : 0.00000 Y :868
## SA:960 3rd Qu.:250000 3rd Qu.:49.00 3rd Qu.: 35000 3rd Qu.: 0.96857 NA's:168
## Max. :250000 Max. :70.00 Max. :200000 Max. : 2.04859
## NA's :1 NA's :98 NA's :17
RQ2: Does education level vary depending on gender of respondent?
Note: We are using gender for explaining variable “sex”.
Assumptions: - Observations must be independent. - Up to 20 % of the expected frequencies can be between 1 and 5, none of them less than 1.
H0: There is no association between categorical variables education and gender. H1: There is association between categorical variables education and gender.
results <- chisq.test(mydata2$education, mydata2$sex,
correct = FALSE) # We skip Yates' correction!
results
##
## Pearson's Chi-squared test
##
## data: mydata2$education and mydata2$sex
## X-squared = 18.219, df = 2, p-value = 0.0001106
addmargins(results$observed)
## mydata2$sex
## mydata2$education F M Sum
## P 607 500 1107
## PS 199 263 462
## S 566 554 1120
## Sum 1372 1317 2689
round(results$expected, 2)
## mydata2$sex
## mydata2$education F M
## P 564.82 542.18
## PS 235.72 226.28
## S 571.45 548.55
round(results$res, 2)
## mydata2$sex
## mydata2$education F M
## P 1.77 -1.81
## PS -2.39 2.44
## S -0.23 0.23
We reject H0 (p < 0.001). We can conclude that there is association between categorical variables education and gender.
addmargins(round(prop.table(results$observed), 3))
## mydata2$sex
## mydata2$education F M Sum
## P 0.226 0.186 0.412
## PS 0.074 0.098 0.172
## S 0.210 0.206 0.416
## Sum 0.510 0.490 1.000
addmargins(round(prop.table(results$observed, 1), 3), 2)
## mydata2$sex
## mydata2$education F M Sum
## P 0.548 0.452 1.000
## PS 0.431 0.569 1.000
## S 0.505 0.495 1.000
addmargins(round(prop.table(results$observed, 2), 3), 1)
## mydata2$sex
## mydata2$education F M
## P 0.442 0.380
## PS 0.145 0.200
## S 0.413 0.421
## Sum 1.000 1.001
For easier grading I explained first numbers in tables (those on position [1,1] in the above matrixes).
Out of 2689 participants there is 22.6 % (607/2689) of them that are female and have primary education.
Out of all participants who has primary school there is 54.8 % (607/1107) of females.
Out of all participants who are female there is 44.2 % (607/1372) of those who has primary education.
library(effectsize)
effectsize::cramers_v(mydata2$education, mydata2$sex)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.08 | [0.04, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.08)
## [1] "very small"
## (Rules: funder2019)
We can conclude that there is association between categorical variables education and gender, although the effect is very small (|r| = 0.08).