General Guidelines
Homework 1
Stats 20 Lec 1 and 2 Summer 2022
Please use R Markdown for your submission. Include the following files: • Your .Rmd file.
Copyright By PowCoder代写 加微信 powcoder
• The compiled/knitted HTML document.
• The image file of a kitten (used in Question 1(d)).
Name your .Rmd file with the convention 123456789_stats20_hw0.Rmd, where 123456789 is replaced with your UID and hw0 is updated to the actual homework number. Include your first and last name and UID in your exam as well. When you knit to HTML, the HTML file will inherit the same naming convention.
The knitted document should be clear, well-formatted, and contain all relevant R code, output, and explanations. R code style should follow the Tidyverse style guide: https://style.tidyverse.org/.
Any and all course material, including these homework questions, may not be posted online or shared with anyone at any time without explicit written permission by the instructor ( ), even after the quarter is over. Failure to comply is a breach of academic integrity.
Note: Questions 3–8 on this homework should be done using only functions or syntax discussed in Chapter 1 of the lecture notes. No credit will be given for use of outside functions.
Basic Questions
Collaboration on basic questions must adhere to Level 0 collaboration described in the Stats 20 Collaboration Policy.
Question 1
The objective of Question 1 is to build familiarity with RStudio and basic R Markdown syntax.
Hint: The “Write with Markdown” section of the R Markdown Cheat Sheet may be useful for this question.
Reproduce the following passage of text, including the italicized and bolded font.
“Sometimes you have to make the hardest climb to see the most beautiful sunrise. I read that once on an old lady’s decorative pillow, but it is really how I feel today. I’ve climbed a very weird and rocky mountain, and it was a pain in the ass, and my legs are tired, and I’m starving. But the sun is rising over a sea of love and waffles and possibility. So I’m just gonna relax and take a deep breath and enjoy this view for as long as I possibly can.” – (from Parks and Recreation)
Using R Markdown, recreate the following table. You should not need R to create this table. Homework Midterm Final Exam Final Project
18% 30% 40% 12%
Note: Tables knitted to HTML may look different from those knitted to PDF, so the formatting may vary. The idea is to get practice creating a table with specified columns and formatting (e.g., left-, center-, or right-justified).
It may look like this:
In a nested list, recount your UCLA class history to date (sorry seniors!), e.g.:
∗ Stats 10
∗ Basket Weaving 33AH
∗ Physics 1: Women’s Lasers
∗ Advanced Parks and Recreation Seminar ∗ Foods 2: Carob Cookies and Berries
∗ Reading 0: The Alphabet
If you have not yet taken classes at UCLA (other than this one), include your most recent year’s class history.
Note: The bullet points do not need to be identical to those in the example. Nested lists knitted to HTML may look different from those knitted to PDF.
It may look like this:
Find a picture of a kitten (not this one) and include it in your document. Be sure to cite your source.
Sample kitten from: https://bit.ly/3rGWPhm
Question 2
Watch the following videos:
• https://youtu.be/cDA3_5982h8
(For anyone who needs it, a transcript can be found here: https://bit.ly/2Qw58xx)
• https://youtu.be/NywzrUJnmTo
(For anyone who needs it, a transcript can be found here: https://bit.ly/3ubryVB)
What do you think is the relevance of these videos to you in the context of this class? Explain as best you can.
In a structured way, write detailed instructions to make a grilled cheese sandwich.
Note: If you do not know how to make a grilled cheese sandwich, you are allowed to reference a recipe online (or elsewhere). Your detailed instructions must still follow the collaboration policy (do not just copy the
recipe verbatim), and using any outside sources must be cited.
Question 3
The objective of this question is to give practice with creating code chunks, calculator computation, and using basic functions.
Use R to compute the following quantities. Include parentheses to clarify the order of operations. (i) 68
165 (ii) 101−87
(iii) 8+3∗π−3+4/2∗−1
Use the built-in functions in R to compute the following quantities.
(i) log2 54 √
(iii) eπ + e−π
(i) Find the number of times that 25 goes into 119. (ii) Find the remainder after dividing 119 by 25.
Question 4
The objective of this question is to give practice with object assignment and writing a function with one argument.
Recall that the volume of a sphere of radius r is V = 4πr3. 3
(i) Find the volume of a sphere of radius r = 1. Store the output as an object called vol_1. (ii) Find the volume of a sphere of radius r = 4. Store the output as an object called vol_4.
Write a function called vol_sphere() that inputs an argument r and outputs the volume of a sphere of radius r. Set the default of r to be 1.
(i) Verify that the command vol_sphere() (i.e., calling the function without changing the default argument) computes the same value as vol_1.
(ii) Use the vol_sphere() function to compute the volume of a sphere of radius 4, and verify that the result is the same value as vol_4.
Note: For those who have learned relational operators (which are not allowed on this assignment anyway), verification does not require logical comparison. For example, just show that the output of vol_sphere() appears to be the same value as vol_1 when printed to the console (or knitted in a code chunk).
Question 5
The objective of this question is to give practice with writing a function with several arguments and creating local objects.
Write a function called z_prop() which calculates the z-statistic given the following values/arguments:
x: the number of successes
n: the number of trials
p0: the hypothesized population proportion
Hint: As a reminder, the z-statistic is given by the formula
z = p0 (1 − p0) ,
where pˆ denotes the sample proportion. How is pˆ related to the input arguments?
Suppose that 60% of residents in the city of Pawnee, Indiana, prefer waffles over pancakes. Out of a sample of 13 Pawnee residents, 10 of them prefer waffles. Use your z_prop() function from (a) to calculate the z-statistic for this sample.
Hint: The input argument p0 is a proportion, not a percentage.
Interpret the value of the z-statistic from (b) in the context of the data. Hint: This is a Stats 10/12/13 question.
For the same population of Pawnee residents, another sample is observed. Out of a sample of 39 Pawnee residents, 30 of them prefer waffles. Use your z_prop() function from (a) to calculate the z-statistic for this sample. How/Why has your z-statistic changed?
Intermediate Questions
Collaboration on intermediate questions must adhere to Level 1 collaboration described in the Stats 20 Collaboration Policy.
Question 6
The objective of this question is to give further practice with writing a function with several arguments and following instructions given in pseudocode.
Read through the following pseudocode and answer the following questions:
FUNCTION: lease_calc
INPUTS: msrp : the manufacturer’s suggested retail price.
price: the sale price of the car in dollars.
down: the amount of the down payment on the car in dollars.
n: the duration of the lease in months
default = 36.
res: the residual percentage, what the expected value of the car at the end of
the lease relative to its price, a percentage represented in decimal form
default = 0.60.
mf: the “money factor” of the lease, essentially a finance charge
default = 0.001.
tax: the local sales tax rate as a decimal
default = 0.095.
OUTPUT: The monthly lease payment on a new car.
capitalized cost
residual value
monthly depreciation
<- sale price of the car - down payment
<- manufacturer's suggested retail price * residual percentage
<- difference of capitalized cost and residual value then
divided by the number of months
monthly finance charge <- capitalized cost plus residual value then multiplied
by the money factor
sub-total <- monthly depreciation + monthly finance charge
total <- sub-total plus taxes
OUTPUT total
Based on the above pseudocode, write a function called lease_calc() to calculate the monthly lease payment on a new car.
Use your lease_calc() function from (a) to calculate the monthly lease payment on a car with an MSRP of $31,495 sold for $29,895, with a $2,500 down payment, over 36 months, with a residual percentage of 52%, at a money factor of 0.0016, in Los Angeles where the local sales tax is 9.5%.
Question 7
The objective of this question is to give practice with breaking up a problem into steps and encapsulating the algorithm into a function.
For this question, we will refer to U.S. coins of denominations 1, 5, 10, and 25 cents, respectively called pennies, nickels, dimes, and quarters.
Without using R, what is the minimum number of coins required to equal 47 cents? Explain your reasoning.
Hint: For example, using 47 pennies would take 47 coins. Using 9 nickels and 2 pennies would take 11 coins. How would you find the minimum number of coins needed?
We want to formalize and generalize the logic you used in (a). For an arbitrary number of cents, outline or describe the steps you would take to find the minimum number of coins required to equal that number of cents.
Hint: This is meant to be done without R, but you will want to be clear so that you can translate it relatively easily into R code in (c).
Using your outline from (b), write a function called get_minimum_coins() which inputs a positive (whole) number of cents (call the argument cents) and outputs the minimum number of coins required to equal that number of cents.
Hint: Consider what mathematical operators we have introduced in Chapter 1 that may be helpful here.
Test your get_minimum_coins() function from (c) with the following inputs: (i) 31 cents (Answer: 3 coins)
(ii) 48 cents (Answer: 6 coins) (iii) 139 cents (Answer: 10 coins)
Use and explain your reasoning to find the number of cents less than 100 which requires the most coins. Verify your answer with your get_minimum_coins() function from (c).
Question 8
The objective of this question is to help you learn about the scope of global and local objects, the logic of functions, and the concept of masking.
Note: If you collaborate with others on this question, please use extra caution not to give away the answer so that everyone has an opportunity to think deeply about this question. This is meant to be a more challenging/conceptual question, so do not be discouraged if you do not fully solve this question right away.
Consider the following code:
x <- 3 y <- 4
exp1 <- function() { x^y
exp2 <- function(x = 2) { x^y
exp_y <- function(x) { function(y) {
Why does the command exp1() give a different answer than exp2()?
Why does the command exp_y() not throw an error? Explain in detail what is happening. Hint: Does exp_y() output a numeric value? What does it output?
Without redefining the global objects x or y, changing the definition of the exp_y() function, or creating any new objects, use the exp_y() function to calculate the fourth power of fifteen.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com