Ivana Tonevska

Homework 2

#data(package = .packages(all.available = TRUE))
#install.packages("carData")
library(carData)
mydata <- Salaries
head(mydata)
##        rank discipline yrs.since.phd yrs.service  sex salary
## 1      Prof          B            19          18 Male 139750
## 2      Prof          B            20          16 Male 173200
## 3  AsstProf          B             4           3 Male  79750
## 4      Prof          B            45          39 Male 115000
## 5      Prof          B            40          41 Male 141500
## 6 AssocProf          B             6           6 Male  97000

Explanation of the dataset ‘Salaries for Professors’:

- Source: Fox J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
- Data for a nine-month academic salary for Assistant Professors, Associate Professors and Professors in a college in the U.S during 2008-09.
- Unit of obesrvation: one professor in a college in the US
- Sample size: 397 professors
- 6 variables:
  * rank:role of the individual in the college (Assistant Professor, Associate Professor or Professor) (categorical, ordinal)
  * discipline: A = 'theoretical' departments; B = 'applied' departments (categorical, binomial)
  * yrs.since.phd: years since PhD (numerical, ratio)
  * yrs.service: years of service (numerical, ratio)
  * sex: sex of the individual (Male or Female) (categorical)
  * salary: nine-month salary, in dollars (numerical, ratio)
  

Descriptive statistics:

nrow(mydata)
## [1] 397
colnames(mydata)[1] <- "Status" #changing the name of the first column from 'rank' to 'Status'
class(mydata$Status)
## [1] "factor"
class(mydata$sex)
## [1] "factor"
summary(mydata$Status)
##  AsstProf AssocProf      Prof 
##        67        64       266
mydata$disciplineF <- factor(mydata$discipline,
                             levels = c("A", "B"),
                             labels = c("theoretical", "applied"))
#Creating a new variable, disciplineF, and assigning values for 'A' and 'B'
class(mydata$disciplineF)
## [1] "factor"
summary(mydata)[ , -2 ]
##        Status    yrs.since.phd    yrs.service        sex     
##  AsstProf : 67   Min.   : 1.00   Min.   : 0.00   Female: 39  
##  AssocProf: 64   1st Qu.:12.00   1st Qu.: 7.00   Male  :358  
##  Prof     :266   Median :21.00   Median :16.00               
##                  Mean   :22.31   Mean   :17.61               
##                  3rd Qu.:32.00   3rd Qu.:27.00               
##                  Max.   :56.00   Max.   :60.00               
##      salary            disciplineF 
##  Min.   : 57800   theoretical:181  
##  1st Qu.: 91000   applied    :216  
##  Median :107300                    
##  Mean   :113706                    
##  3rd Qu.:134185                    
##  Max.   :231545
  • The highest nine-month salary earned is 231.545dollars, while the average is 113.706dollars
  • yrs.since.phd; median = 21, meaning that half of the professors have had their PhD for less than, or exactly 21 years
  • salary; 3rd Qu = 134185, meaning that 75% of the professors have a salary lower than, or equal to 134185
#install.packages("pastecs")
library(pastecs)

round(stat.desc(mydata)[ , c(-1,-2,-5,-7)], 2)
##          yrs.since.phd yrs.service       salary
## nbr.val         397.00      397.00       397.00
## nbr.null          0.00       11.00         0.00
## nbr.na            0.00        0.00         0.00
## min               1.00        0.00     57800.00
## max              56.00       60.00    231545.00
## range            55.00       60.00    173745.00
## sum            8859.00     6993.00  45141464.00
## median           21.00       16.00    107300.00
## mean             22.31       17.61    113706.46
## SE.mean           0.65        0.65      1520.16
## CI.mean           1.27        1.28      2988.60
## var             166.07      169.16 917425865.05
## std.dev          12.89       13.01     30289.04
## coef.var          0.58        0.74         0.27
  • The mean for ‘yrs.since.phd’ which equals to 22.31, shows that the professors had their PhD for an average of 21 years.
  • std.dev for ‘salary’ (30289.04) shows the average deviation of individual salaries from the mean salary.
  • The range of ‘yrs.since.phd’ is 55 (with min=1 & max=56), indicating that the years since earning a Ph.D. range from 1 to 56 years
  • nbr.na for yrs.service = 11, those 11 individuals have been in service for less than a year.
#install.packages("psych")
library(psych)
describe(mydata$salary)
##    vars   n     mean       sd median  trimmed      mad   min    max
## X1    1 397 113706.5 30289.04 107300 111401.6 29355.48 57800 231545
##     range skew kurtosis      se
## X1 173745 0.71     0.18 1520.16
head(mydata)
##      Status discipline yrs.since.phd yrs.service  sex salary
## 1      Prof          B            19          18 Male 139750
## 2      Prof          B            20          16 Male 173200
## 3  AsstProf          B             4           3 Male  79750
## 4      Prof          B            45          39 Male 115000
## 5      Prof          B            40          41 Male 141500
## 6 AssocProf          B             6           6 Male  97000
##   disciplineF
## 1     applied
## 2     applied
## 3     applied
## 4     applied
## 5     applied
## 6     applied

Reasearch Question 1

What is the correlation between Years in service and the Salary of the units observed?

I will be further analysing the relationship between these two variables as I expect to see positive correlation between years in service and salary.
#install.packages("car")
library(car)
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit

Assumptions:

  • Both variables are numerical
  • Linear relationship between the variables
  • Errors are normally distributed
  • (n = 397)
library(car)
scatterplot(mydata$yrs.service, mydata$salary, 
            smooth = TRUE,
            xlim = c(0, 65),
            ylim = c(55000, 235000),
            main = "Relationship between Salary and Years in service",
            xlab = " Years in service", 
            ylab = "Salary")

Linearity is vialated and we shouldn’t use Pearson Coefficient Correlation (both variables are numeric) but for educational purposes, I will assume that it is still valid and continue with the analysis.

The sample is sufficiently large (n=397) so we assume normality

#install.packages("Hmisc")
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following object is masked from 'package:psych':
## 
##     describe
## The following objects are masked from 'package:base':
## 
##     format.pval, units
cor(mydata$salary, mydata$yrs.service,
    method = "pearson",
    use = "complete.obs")
## [1] 0.3347447
We assume there is a linear relationship between Years in service and salary. Their correlation is weak and positive.
cor.test(mydata$yrs.service, mydata$salary,
         method = "pearson",
         exact = FALSE,
         use ="complete.obs")
## 
##  Pearson's product-moment correlation
## 
## data:  mydata$yrs.service and mydata$salary
## t = 7.0602, df = 395, p-value = 7.529e-12
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.2443740 0.4193506
## sample estimates:
##       cor 
## 0.3347447
  • H0: rho (years in service, salary) = 0
  • H1: rho (years in service, salary) != 0
  • We reject H0 at p<0.001. There is correlation (linear relationship) between years in service and salary.

Reasearch Question 2

What is the association between the status of the professors and their gender?

I will be further analysing the relationship between these two variables to see if there is association between the gender and their status.
Assumptions:
  1. Observations must be independent.
  2. Expected frequencies are >1.
  3. Up to 20% of the expected frequencies can be between 1 and 5 (however, this will reduce the power of the test).
results <- chisq.test(mydata$Status, mydata$sex,
                      correct = TRUE)

results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$Status and mydata$sex
## X-squared = 8.5259, df = 2, p-value = 0.01408
  • H0: There is no association between status and gender.
  • H1: There is association between status and gender.
  • We do not have enough evidence to reject H0 (p=0.015) and cannot claim that there is association between gender and status.
addmargins(results$observed)
##              mydata$sex
## mydata$Status Female Male Sum
##     AsstProf      11   56  67
##     AssocProf     10   54  64
##     Prof          18  248 266
##     Sum           39  358 397
addmargins(round(results$expected, 2))
##              mydata$sex
## mydata$Status Female   Male Sum
##     AsstProf    6.58  60.42  67
##     AssocProf   6.29  57.71  64
##     Prof       26.13 239.87 266
##     Sum        39.00 358.00 397
2nd and 3rd asusmptions are met.
round(results$res, 2)
##              mydata$sex
## mydata$Status Female  Male
##     AsstProf    1.72 -0.57
##     AssocProf   1.48 -0.49
##     Prof       -1.59  0.52
addmargins(round(prop.table(results$observed), 3))
##              mydata$sex
## mydata$Status Female  Male   Sum
##     AsstProf   0.028 0.141 0.169
##     AssocProf  0.025 0.136 0.161
##     Prof       0.045 0.625 0.670
##     Sum        0.098 0.902 1.000
  • Out of 397 professors, there are 14.1% of Assistant Professors who are male.
addmargins(round(prop.table(results$observed, 1), 2), 2)
##              mydata$sex
## mydata$Status Female Male  Sum
##     AsstProf    0.16 0.84 1.00
##     AssocProf   0.16 0.84 1.00
##     Prof        0.07 0.93 1.00
  • Out of all professors who are Associate Professors, 16% are female.
addmargins(round(prop.table(results$observed, 2), 3), 1)
##              mydata$sex
## mydata$Status Female  Male
##     AsstProf   0.282 0.156
##     AssocProf  0.256 0.151
##     Prof       0.462 0.693
##     Sum        1.000 1.000
  • Out of all professors who are male, 15.1% are Associate Professors.
#install.packages("effectsize")
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize::cramers_v(mydata$Status, mydata$sex)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.13              | [0.00, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.13)
## [1] "small"
## (Rules: funder2019)

There is small effect of gender on the status of the professors (r=0.13).