0% found this document useful (0 votes)
2 views

Homework-1

This document outlines the guidelines and requirements for Stats 20 Homework 1 at UCLA, including submission formats and naming conventions for files. It consists of various questions aimed at familiarizing students with R Markdown, R coding, and statistical concepts, along with specific tasks such as recreating text, tables, and images. Collaboration policies and academic integrity rules are emphasized throughout the document.

Uploaded by

8pwj6mpj8j
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Homework-1

This document outlines the guidelines and requirements for Stats 20 Homework 1 at UCLA, including submission formats and naming conventions for files. It consists of various questions aimed at familiarizing students with R Markdown, R coding, and statistical concepts, along with specific tasks such as recreating text, tables, and images. Collaboration policies and academic integrity rules are emphasized throughout the document.

Uploaded by

8pwj6mpj8j
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Stats 20 Homework 1

Thomas Maierhofer

UCLA Department of Statistics & Data Science

General Guidelines

Please use R Markdown for your submission. The files you submit will be:

1. 123456789_stats20_hw1_First_Last.Rmd
Take the provided template R Markdown file and make the necessary edits so that it will generate the
requested output. You will also need to go through the rest of the file and insert your answers to any
questions. Submit this file to Canvas/BruinLearn.
2. 123456789_stats20_hw1_First_Last.pdf
Your output PDF file. This is the primary file that will be graded. Make sure all requested output is
visible in the output file. Submit this file to Gradescope.
3. my_kitten.png or something similar. This is the image file of the kitten you are including in your
document.

Name your .Rmd file with the convention 123456789_stats20_hw0_First_Last.Rmd, where 123456789 is
replaced with your UID, hw0 is updated to the actual homework number, and First_Last with your actual
first and last name. When you knit to PDF, the PDF file will inherit the same naming convention.
The knitted document should be clear, well-formatted, and contain all relevant R code, output, and expla-
nations. 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 (Thomas
Maierhofer), 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. We do not
cover this in class, you will have to learn how to do this in your own time.
Hint: The “Write with Markdown” section of the R Markdown Cheat Sheet may be useful for this question.

1
(a)

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.” – Leslie Knope (from Parks and Recreation)

2
(b)

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:

(c)

In a nested list, recount your UCLA class history to date (sorry seniors!), e.g.:

• 2018
• Fall
• Stats 10
• Basket Weaving 33AH
• Physics 1: Women’s Lasers
• Winter
• Advanced Parks and Recreation Seminar
• Foods 2: Carob Cookies and Berries
• Reading 0: The Alphabet
• etc. . .

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:

3
(d)

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)

(a)

What do you think is the relevance of these videos to you in the context of this class? Explain as best you
can.

(b)

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.

4
Question 3

The objective of this question is to give practice with creating code chunks, calculator computation, and
using basic functions.

(a)

Use R to compute the following quantities. Include parentheses to clarify the order of operations.

68
(i)
165
8
(ii) 101− 7
(iii) 8 + 3 ∗ π − 3 + 4/2 ∗ −1

(b)

Use the built-in functions in R to compute the following quantities.

(i) log3 54

2
(ii) ee
eπ + e−π
(iii)
2

(c)

(i) Find the number of times that 25 goes into 119.

(ii) Find the remainder after dividing 119 by 25.

5
Question 4

The objective of this question is to give practice with object assignment.


4 3
Recall that the volume of a sphere of radius r is V = πr .
3

(a)

(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.

6
Question 5

The objective of this question is to give practice with writing mathematical functions with several arguments
as R code and creating local objects.

(a)

Write code to calculate the z-statistic (save it in an object called z_prop) given the following values:

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


p̂ − p0
z=r ,
p0 (1 − p0 )
n
where p̂ denotes the sample proportion. How is p̂ related to the input arguments?
You may use the following test inputs and their outcome z = −1 to verify that your code is working.

# test inputs
x <- 10
n <- 25
p0 <- 0.5
# your code here
# verify that z_prop should be -1
z_prop

(b)

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 code from (a) to calculate the z-statistic for this
sample.
Hint: The input p0 is a proportion, not a percentage.

(c)

Interpret the value of the z-statistic from (b) in the context of the data.
Hint: This is a Stats 10/12/13/15 question.

(d)

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 code from (a) to calculate the z-statistic for this sample.
How/Why has your z-statistic changed?

7
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 code with several arguments and
following instructions given in pseudocode.
Read through the following pseudocode and answer the following questions:

CODE: 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 <- sale price of the car - down payment


residual value <- manufacturer’s suggested retail price * residual percentage
monthly depreciation <- 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 code to calculate the monthly lease payment on a new car. 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%.

8
Question 7

The objective of this question is to give practice with breaking up a problem into steps and encapsulating
the algorithm into R code.
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.

(a)

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?

(b)

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).

(c)

Using your outline from (b), write code that inputs a positive (whole) number of cents (call the argument
cents) and computes 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.

[1] 5

(d)

Test your code from (c) with the following inputs:

(i) 32 cents (Answer: 4 coins)

(ii) 49 cents (Answer: 7 coins)

(iii) 137 cents (Answer: 8 coins)

(e)

Use and explain your reasoning to find the number of cents less than 100 which requires the most coins.
Verify your answer with your code from (c).

Question 8

What are your goals for this class? Please be brief, bullet points are totally fine.

You might also like