# If you don't already have the tidyverse library installed,
# you will need to type install.packages("tidyverse") into the Console
library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── 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

1. Changing the author field and file name.

(a) Change the author: field on the Rmd document from Your Name Here to your own name.
(b) Rename this file to “lab01_YourHameHere.Rmd”, where YourNameHere is changed to your own name.

2. Hello World!

Here’s an R code chunk that prints the text ‘Hello world!’.

print("Hello world!")
## [1] "Hello world!"

(a) Modify the code chunk below to print your name

print ("Tristan Greene")
## [1] "Tristan Greene"


### 3. CEO data

We’ll look at data frame and plotting in much more detail in later classes. For a previous of what’s to come, here’s a very basic example.

For this example we’ll use a very simple dataset. The CEO data comes with the default installation of the wooldridge package.

#install.packages("wooldridge")
data(ceosal1, package='wooldridge')

You can look at the whole dataset by typing ‘View(ceosal1)’ If you want to know more about the data itself see page 32 of https://cran.r-project.org/web/packages/wooldridge/wooldridge.pdf


To see the first few columns of the data, just type `head(ceosal1)`.



Now let's get some summary statistics on the salary itself


``` r
# sample average:
mean(ceosal1$salary)
## [1] 1281.12
# sample median:
median(ceosal1$salary)
## [1] 1039
#standard deviation:
sd(ceosal1$salary)
## [1] 1372.345
# summary information:
summary(ceosal1$salary)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     223     736    1039    1281    1407   14822

1281.12 1039 1372.345 Min. 1st Qu. Median Mean 3rd Qu. Max. 223 736 1039 1281 1407 14822

we can also get the correlation with another variable return on equity # correlation with ROE: cor(ceosal1\(salary, ceosal1\)roe) 0.1148417

We can easily produce a histogram of ceo salary using the hist function. hist(ceosal1$salary)




last let's build a scatterplot of salary and roe with the plot function
plot(ceosal1$salary , ceosal1$roe)

<img src="lab01_351_files/figure-html/q3-scatter-roe-1.png" width="672" />

A nice thing of R is you can draw two scatterplots on the same graph plot(ceosal1\(salary, ceosal1\)roe, main=“Observation of CEOs”, xlab=“CEO Salary”, ylab=“ROE in Red ROS in Blue”, col=“red”, cex=2) points(ceosal1\(salary, ceosal1\)ros, col=“blue”, cex=2)





next we will do a two sided t-test to see if the CEO salary differs in it is a consumer product company (variable is consprod and is binary). In the next lab you will learn how to construct binary variables. But one thing to note in the next command is rather than continuously type ceosal1$variable_name--i just say where the dataset is at the end of the line. This is the BIG advantage of R over other software packages is that you can have many datasets in memory.

ttest.consprod <- t.test(salary ~ consprod, data = ceosal1) 
ttest.consprod



``` r
# Make sure the dataset is loaded earlier:
# data(ceosal1, package = "wooldridge")

# Safety: check the variable exists (some versions name it differently)
if (!"consprod" %in% names(ceosal1)) {
  stop("Variable 'consprod' not found in ceosal1. Run names(ceosal1) to confirm the column name.")
}

# Two-sample (unpaired) t-test: salary by consumer-product firm (0/1)
ttest.consprod  <- t.test(salary ~ consprod, data = ceosal1)
ttest.consprod
## 
##  Welch Two Sample t-test
## 
## data:  salary by consprod
## t = -2.0939, df = 64.077, p-value = 0.04023
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -1209.55658   -28.44454
## sample estimates:
## mean in group 0 mean in group 1 
##        1103.416        1722.417


we can also do the same test and construct a different sized confidence interval say 98% rather than the default 95%
ttest.consprod2 <- t.test(salary ~ consprod, conf.level = 0.98, data = ceosal1) 
ttest.consprod2
Interpret the results. in the future we will use the ggplot2 function to make data visualizations.



``` r
ttest.consprod2 <- t.test(salary ~ consprod, conf.level = 0.98, data = ceosal1)
ttest.consprod2
## 
##  Welch Two Sample t-test
## 
## data:  salary by consprod
## t = -2.0939, df = 64.077, p-value = 0.04023
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 98 percent confidence interval:
##  -1324.34009    86.33898
## sample estimates:
## mean in group 0 mean in group 1 
##        1103.416        1722.417

INTERPRETATION: At the 5 percent level, the p value is below 0.05, so reject H0. However, at the 2% level, p value is not below, so fail to reject null.

4. New data and analysis

In this part, I want you to use the cps91 data that is also part of the wooldridge package You will tell me about how many rows (observations) and columns (variables) are in this dataset that is from the US Current Population survey used to calculate monthly labour market statistics including the unemployment rate. husearns is the variable name for husband’s earnings and please give me some summary statistics on this variable like the mean, standard deviation, median, etc. Then earns is the variable name for the wife’s earnings so draw a scatterplot and report how correlated earnings are between husbands and wives. Last, please conduct a ttest if there is a difference in earnings between husbands earnings and wifes’ earnings. data(‘cps91’, package=‘wooldridge’)

data("cps91", package = "wooldridge")
# peek at names if needed:
names(cps91)[1:20]
##  [1] "husage"   "husunion" "husearns" "huseduc"  "husblck"  "hushisp" 
##  [7] "hushrs"   "kidge6"   "earns"    "age"      "black"    "educ"    
## [13] "hispanic" "union"    "faminc"   "husexp"   "exper"    "kidlt6"  
## [19] "hours"    "expersq"
dim(cps91)   # rows, columns
## [1] 5634   24
mean(cps91$husearns, na.rm = TRUE)
## [1] 453.5406
median(cps91$husearns, na.rm = TRUE)
## [1] 418.5
sd(cps91$husearns, na.rm = TRUE)
## [1] 406.9878
summary(cps91$husearns)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0     0.0   418.5   453.5   675.0  1923.0

cor(cps91$husearns, cps91$earns, use = "complete.obs")
## [1] 0.1643954
t_paired <- t.test(cps91$husearns, cps91$earns, paired = TRUE)
t_paired
## 
##  Paired t-test
## 
## data:  cps91$husearns and cps91$earns
## t = 37.067, df = 5633, p-value < 2.2e-16
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  209.0350 232.3803
## sample estimates:
## mean difference 
##        220.7077