library(wooldridge)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
2
df_bwght <- wooldridge::bwght
2.1 How many women are in the sample, and how many report smoking
during pregnancy?
cigarettes <- df_bwght$cigs
nrow(df_bwght[cigarettes > 0,])
## [1] 212
There are as many as rows/observations 1388
212 of them smoked during pregnancy
2.2 What is the average number of cigarettes smoked per day? Is the
average a good measure of the “typical” woman in this case?
Explain.
mean(cigarettes)
## [1] 2.087176
No, because only a smaller % of women smoked during pregnancy
2.3 Among women who smoked during pregnancy, what is the average
number of cigarettes smoked per day? How does this compare with your
answer from part (ii), and why?
smokers_women <- df_bwght[cigarettes > 0,]
mean(smokers_women$cigs)
## [1] 13.66509
The average per women smoking was 13.66509
2.4 Find the average of motheduc in the sample. How many
observations are missing data on mother’s education?
df_bwght_noNa <- df_bwght %>% drop_na()
mean(df_bwght_noNa$motheduc)
## [1] 13.1251
Removing the single NA (for motheduc) observation we find the
average of years of education is 13.1251
2.5 Report the average family income and its standard deviation.
What is the unit of family income?
mean(df_bwght$faminc)
## [1] 29.02666
sd(df_bwght$faminc)
## [1] 18.73928
The average income is 29.02666 thousand $ per year
18.73928 of Standard Deviation
3
df_meap <- wooldridge::meap00_01
3.1 Find the largest and smallest values of math4. Does the range
make sense? Explain.
math_scores <- df_meap$math4
max(math_scores)
## [1] 100
min(math_scores)
## [1] 0
It has a maximum of 100 and minimum of 0 because it represents a %
in student pass rates per school
3.2 How many schools have a perfect pass rate on the math test? What
percentage is this of the total sample?
nrow(df_meap[math_scores == 100,])
## [1] 37
nrow(df_meap[math_scores == 100,])*100/nrow(df_meap)
## [1] 2.186761
It’s 37 schools with 100% pass rates, 2.186761 % of all 1692
observations
3.3 How many schools have math pass rates of exactly 50%?
nrow(df_meap[math_scores == 50,])
## [1] 12
12 Of them have 50% pass rates for math
3.4 Compare the average pass rates for the math and reading scores.
Which test is harder to pass?
read_scores <- df_meap$read4
mean(math_scores)
## [1] 72.68777
mean(read_scores)
## [1] 60.68989
72.68777 % average passrates for math, 60.68989 % average for
reading. Reading is harder.
3.5 Find the correlation between math4 and read4. What do you
conclude?
cor(math_scores, read_scores)
## [1] 0.8344536
With 83.44536 % of correlation for both columns I think there’s a
correlation between passing math and reading.
3.6 The variable exppp is expenditure per pupil. Find the average of
exppp along with its standard deviation. Would you say there is wide
variation in per pupil spending?
mean(df_meap$exppp)
## [1] 5264.037
sd(df_meap$exppp)
## [1] 1003.533
Average spending is $5264.037, while the SD is $1003.533. It’s
understandable given there’s different schools with different
services.
3.7 Suppose School A spends $6,000 per student and School B spends
$5,500 per student. By what percentage does School A’s spending exceed
School B’s? Compare this to 100 · [log(6,000) – log(5,500)], which is
the approximation percentage difference based on the difference in the
natural logs. (See Section A.4 in Appendix A.)
var_per <- (6000 - 5500)/5500
var_per * 100
## [1] 9.090909
var_per_log <- log(6000) - log(5500)
var_per_log * 100
## [1] 8.701138
((var_per - var_per_log))*100
## [1] 0.3897714
9.090909 % percentage variation and 8.701138 of logarithmic
variation,a 0.3897714 % between them