Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Problem Set 1 - EC420

August 2023

Problem Set #1 is due on the date listed in the syllabus calendar.  No exceptions

Turn in a knitted .pdf only on D2L. No pdf-from-HTML.

This assignment is designed to get you working Rmarkdown, and to review concepts from our Stats Review. Part of the purpose is to make sure your computer is running R, RStudio, and Rmarkdown correctly.  Do not wait until the last minute to do this problem set.

•  For each of the numbered main headers in this document, create a main section header in your mark- down document using a double ##, as shown in the template.  The ## must be at the start of a line

- no spaces before it.

• When there are subsections (e.g. “A” and “B”), then use triple ###’s, as shown in the template.

• When the blue Question appears on the assignment, make sure you answer the question in writing after your coding chunk.

As you work, click ‘knit’ in the toolbar to make sure your document renders. Check often so you catch any errors early!

Before you start

•  Make sure you have watched all of the R videos on MediaSpace.   R  Part  1 shows you how to in- stall packages that you’ll need to complete this assignment.  R Part 3 shows you a bit about using Rmarkdown. These are mandatory for completing your assignment.

•  Make sure you have worked through the Getting_Started_Rscript.R file used in the R videos on MediaSpace.

•  Check for and install (if needed) tinytex

Checking and Installing tinytex

•  To check if tinytex was installed when you installed the rmarkdown package, open R and directly in the console, type Sys.which( ' pdflatex ' )

• If this returns a filepath (like /usr/bin C:\\Users or anything that isn’t just  '' ), then you’re good to go! If not, read on. . .

•  Installing tinytex if not present:

1.  Close Rstudio

2.  Open terminal (if on a mac) or CMD if on a Windows machine


3.  Type R  --vanilla in the terminal/CMD

4.  Once an instance of R launches inside your terminal/CMD, type install.packages( ' tinytex ' )

5.  Once the package tinytex is installed, type tinytex::install_tinytex().  This will install a very light version of latex on your computer.

6.  Once it completes successfully, close the terminal, and launch Rstudio.

7.  Check for a path using Sys.which( ' pdflatex ' )

If you are still having trouble, consult myself via Slack and consider using Rstudio.cloud - see the sidebar here

And finally, before you start

•  Once you know you have tinytex installed, open the template, and **before you do anything, click on the "knit" button.** If you get an error and no PDF appears, then check to make sure you have followed all directions, installed knitr and rmarkdown (see the R videos on MediaSpace) and correctly installed tinytex. Come to office hours or use the Slack if you’re stuck.

Now, on to the assignment!


1.  Setup your document

A. Load libraries (2 points)

Using the the chunk labeled q1A in the template, load the wooldridge library, along with knitr (already on the template). Then, in that code chunk, add a line that creates an object called wage2 using the following code: wage2  =  wooldridge::wage2.  The object wage2 will now exist in R’s memory and we can use it in subsequent sections.

Question: In the R videos on MediaSpace, you are asked to install multiple packages. Name three of them here.

B. Show filepath (2 points)

In the next code chunk, run the code rstudioapi::documentPath().  Here, the function documentPath() from the automatically-loaded rstudioapi package returns the path of your problem set file.  Make sure you have followed instructions and have saved the template with a proper name in a proper folder in a directory you’ve created for this class. It should not show as downloads/EC420_Template.RMD!

No need to write an answer in text afterwards, just the code and it’s output will do.

2.  Standard Error of the Mean

We want to test a hypothesis about the average wage of college graduates (16 years of education).  To do that, we will calculate the sample mean and the standard error of the mean for this group.You can refer to 00 - Stats Review on Sample Variance for help.

A. Subset wage2 (2 points)

You have already created wage2 back in 1A. That R object still exists for subsequent code chunks (note: when you click “knit”, R starts fresh and re-runs each chunk, so make sure every step is included in a prior code chunk).

In a new code chunk labeled q2A, we want to subset wage2 keeping only those rows where wage2$educ==16 (16 years of education).  We are subsetting rows, so our subset index will be inside the square brackets before the comma.

Remember that when you subset a data.frame, you must overwrite the old one: wage2  =  wage2[???,]

Question:  How  many rows does wage2 have once you have subset it?  Use NROW to see.  Using the output from your code, write your answer in words after the code chunk.

B. Calculate the sample mean and sample variance for wage in our sample of wages (6 points)

Note: from here on out, you need to create the chunks in your document.  Make sure you name each chunk with a unique name corresponding to the question and subquestion.

In a new code chunk, create a new object called meanCollege that is equal to the sample mean of wage (wagecollege ) for those observations in the sample (college workers).

Then, create a new object called nCollege that is equal to the number of observations (n) in the sample.

Then, create a new object called varCollege that is equal to the sample variance of wage (s2 ) for the sample. Use the sample variance formula from our notes.

Finally, use print(meanCollege), print(varCollege), and print(nCollege) to output the sample mean, sample variance, and sample size.

Question What are the units of the wage column?  Hint: you can learn about wooldridge datasets by using the help for that data ?wage2

C. Calculate the standard error of the mean (4 points)

Remember, our formula from Section 5 of 00 - Stats Review, replacing  with wage:

sewage  =  

Where sewage  is the standard error of the mean wage for college graduates, s2  is the sample variance, and n is the sample size. You already have objects that hold the sample equivalent of each of the latter two things: varCollege and nCollege.  These sample estimates are used to calculate the standard error of the mean for college workers:  semCollege  =  sqrt(varCollege/nCollege).  Calculate and print the standard error of the mean for our college sample.

3.  Hypothesis testing:  population mean

Someone has claimed that the average wage for college graduates is $1,000. We wish to test this hypothesis H0  : µwage  = 1000 for college-educated workers.  To do so, we will use the result from 2E,  semCollege, as well as the sample mean, meanCollege.

A. Hypothesis testing H0  : the true population  mean wage for college graduates is $1,000  (8 points)

We know the sampling  distribution of wagecollege  and thus we can build a 95% confidence interval around the sample mean wagecollege  and test to see if 1100 is inside that interval.  We will use a two-tailed test, meaning we will reject the null hypothesis if the data is much higher or much lower than the null hypothesis. A two-tailed test has a symmetric confidence interval.

The formula for a two-tailed 95% confidence interval for any random variable (given the mean and std. error) is:

lwagecollege (tcrit  × sewage )    ,    wagecollege + (tcrit  × sewage )]

You already calculated sewage  and saved it as semCollege.  Note that this is  not the sample variance s2 . Don’t mix them up, they describe two different random variables. You also already have wagecollege  saved as meanCollege from 2B. All we need is tcrit.  Since we used sample variance, we know that we need to use a t-distribution with n − 1 degrees of freedom.

In a new code chunk, do the following:

•  Calculate the degrees of freedom dfn  =  nCollege  -  1

•  Find tcrit. We can do this by using R’s qt function, which takes two arguments

  The first argument is the percentile you are looking for.  So if you want the median value of t distributed variable, you’d use .50.  We want the .025th and the .975th (so that we get the 95% confidence interval).

–  The second argument is the degrees of freedom, dfn.

  Put them together: tcrit  =  qt(.975,  dfn)

•  Calculate the lower and upper bounds of the 95% confidence interval and print them in your code chunk.

Based on the 95% confidence interval, we can answer the question “is the population mean wage for college- educated workers equal to $1,000?” by testing the null hypothesis.

Question: What is your answer to this question, in your own words, and why?

Closing

Remember to knit your work to a PDF to upload to D2L no later than 11:59pm on the due date listed on the syllabus calendar.

In your final section, please report the number of hours you spent on this homework assignment.  I use this data to track how intense a problem set is. Future EC420 students will thank you.