Code
library(foreign)
<- read.dta("TeachingRatings.dta") ratings
Make sure you have installed the Quarto CLI and the R packages quarto
and foreign
. To render this qmd file, you have to be in the R console/terminal, load quarto, and then use quarto_render("applied-metrics-1-exercise-set-02-solutions-quarto.qmd")
. Ideally, put the external objects (like data) which need to be loaded in the same folder as your qmd file. Make sure to adjust the paths accordingly.
library(foreign)
<- read.dta("TeachingRatings.dta") ratings
Another approach to adjust paths a bit more automatically is something like paste(getwd(), "TeachingRatings.dta", sep="/")
. This creates a string first by getting the path of the current working directory getwd()
, pasting it with the name of the file "TeachingRatings.dta"
, and then making sure to put a /
in between. Try running this command in the R console to see how it works.
There are some new things you could learn from the qmd file, like how to incorporate results from your code into the text.
lm(course_eval ~ beauty, data = ratings)
Call:
lm(formula = course_eval ~ beauty, data = ratings)
Coefficients:
(Intercept) beauty
3.998 0.133
plot(ratings$beauty, ratings$course_eval, xlab = "Beauty rating", ylab = "Course evaluation")
abline(lm(course_eval ~ beauty, data = ratings))
The average course evaluation score for instructors who scored 0 on the beauty measure is 4. If we compare instructors whose beauty scores differ by 1, their average course evaluation scores will differ by 0.13.
lm(course_eval ~ female, data = ratings)
Call:
lm(formula = course_eval ~ female, data = ratings)
Coefficients:
(Intercept) female
4.069 -0.168
plot(ratings$female, ratings$course_eval, xlab = "Sex (Male = 0, Female = 1)", ylab = "Course evaluation")
abline(lm(course_eval ~ female, data = ratings))
The average course evaluation scores for males and females separately are:
tapply(ratings$course_eval, ratings$female, mean)
0 1
4.069030 3.901026
From the least squares regression, we find that the average course evaluation score for males is 4.07 and this value matches the intercept of the least squares regression of course evaluations on beauty. In addition, female instructors score, on average, 0.17 lower than male instructors. This matches what we see when we take the difference between the two group averages calculated by tapply()
: -0.17.
The difference this time is that female
is a dummy variable, while NetSalesMil
is not. Furthermore, there was just be one comparison between males and females. In the compensation-sales relationship, it might be difficult to find a subset of the data for which NetSalesMil
differ by exactly 1 million dollars. If one uses tapply()
, each possible value of NetSalesMil
becomes a separate category.