Homework_0
txt
keyboard_arrow_up
School
University of Pennsylvania *
*We aren’t endorsed by this school
Course
405
Subject
Statistics
Date
Feb 20, 2024
Type
txt
Pages
4
Uploaded by BaronNarwhal3261
#### STAT 405/705-402 Homework 0, Spring 2022
#### Name:
#### Please list all external sources you have consulted for completing this assignment:
#### Instructions:
#### 1. If a question refers to a function not seen in class, #### use the help facility in R to learn about it.
#### 2. Insert your answers and the R code you used to generate them, beneath each question.
#### Even though for some questions you could find the answer "by inspection", for
this homework you need
#### to write code to get the answers, unless it is explicitly stated that no code
is required.
#### 3. When you are asked to print an answer, it is NOT ENOUGH to simply write a print statement, #### for example print(2*3) as an answer, you also need to #### cut and paste into this document the value(s) that is actually printed (i.e.,6).
#### 4. When submitting to Canvas, please change the file extension to .txt, otherwise it cannot be submitted.
## ------------------------------------------------------------------------
#### Q1. R as a calculator.
#a Store your birthday number, in YYYYMMDD format (example: 20010831), into a numeric variable called bday.
bday <- 20040520
bday
#> bday
#[1] 20040520
# Now write code to find and print the answer for all parts:
#b The cube root of your birthday number.
x <- bday^(1/3)
x
# > x <- bday^(1/3)
# > x
# [1] 271.625
#c The log base 16 of your birthday number.
y <- log (bday, base = exp(16))
y
# > y <- log (bday, base = exp(16))
# > y
# [1] 1.050829
#d Your birthday number, modulo 7. (That's the remainder, after dividing by 7. Use the "%%" operator.)
z <- (bday %% 7)
z
# > z <- (bday %% 7)
# > z
# [1] 3
## ------------------------------------------------------------------------
#### Q2. Some shortcuts for creating vectors.
# In this question, you will learn a few shortcuts for creating vectors of special shapes.
#a The code below creates a vector of all integers from 1 to 10:
vec0 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# This approach would be unwieldy if we want a vector of integers from, say, 1 to 10000. # Use the help facility of R to study the colon (":") operator and understand how it can help with this task.
# Then, create a vector called vec1 consisting of all integers from your birthday number to 20210831.
vec1 <- c(20040520:20210831)
vec1
#b When you read the help page for ":", you may have noticed a function called "seq".
# You can understand its behavior by running the code below:
seq(9.1, 11.1, by = 0.2)
seq(0, 10, length = 30)
# Now use the "seq" function to create a vector called vec2 consisting of all integers divisible by 7
# between 0 and 100 (including 0), in DESCENDING order.
vec2 <- seq(98,0,-7)
vec2
# > vec2 <- seq(98,0,-7)
# > vec2
# [1] 98 91 84 77 70 63 56 49 42 35 28 21 14 7 0
#c Try the following code to learn what the "rep" function does:
rep(c(1, 2), times = 4)
rep(c(0, 1), times = c(3, 5))
# You can also look up the help page for "rep" for additional info.
# Now create a vector called vec3 consisting of all even numbers between 100 and 200 in ascending order, # with the K-th number repeated K times: (100, 102, 102, 104, 104, 104, ...).
# HARD. Hint: Use seq, rep and : within a single line.
vec3 <- rep(seq(100,200,2),seq(1,51,1))
vec3
# > vec3 <- rep(seq(100,200,2),seq(1,51,1))
# > vec3
# [1] 100 102 102 104 104 104 106 106 106 106 108
# [12] 108 108 108 108 110 110 110 110 110 110 112
# [23] 112 112 112 112 112 112 114 114 114 114 114
#list goes on...
## ------------------------------------------------------------------------
#### Q3. Applying functions and operators to vectors.
#a Create the two vectors of numbers alpha = (200, 225, 250,..., 1000)
# beta = (600, 590, 580, ..., 280) vec1 <- seq(200,1000,25)
vec2 <- seq(600, 280,-10)
# > vec1 <- seq(200,1000,25)
# > vec2 <- seq(600, 280,-10)
vec1
vec2
# > vec1
# [1] 200 225 250 275 300 325 350 375 400
# [10] 425 450 475 500 525 550 575 600 625
# [19] 650 675 700 725 750 775 800 825 850
# [28] 875 900 925 950 975 1000
# > vec2
# [1] 600 590 580 570 560 550 540 530 520 510 500
# [12] 490 480 470 460 450 440 430 420 410 400 390
# [23] 380 370 360 350 340 330 320 310 300 290 280
#b What is the sum of the element-wise product of the two vectors?
vec3 <- vec1 + vec2
vec3
# > vec3 <- vec1 + vec2
# > vec3#c What is the maximum value in the vector containing the element-wise products?
# [1] 800 815 830 845 860 875 890 905 920
# [10] 935 950 965 980 995 1010 1025 1040 1055
# [19] 1070 1085 1100 1115 1130 1145 1160 1175 1190
# [28] 1205 1220 1235 1250 1265 1280
#c What is the maximum value in the vector containing the element-wise products?
max(vec3)
# > max(vec3)
# [1] 1280
## ------------------------------------------------------------------------
#### Q4. Applying functions and operators to vectors, continued.
#a Set the random number seed as 20210831.
set.seed(20210831)
#b Create two vectors, named x and y of standard normal random variables, each of length 10000000.
x <- rnorm(10000000,0,1) y <- rnorm(10000000,0,1)
#c Create a third vector, called z, containing the elementwise square root of the sum
# of the squares of the elements.
# That is, if you call the first elements x1 and y1, etc., you need to calculate
# # x1,y1 --->>>> sqrt(x1^2 + y1^2)
# x2,y2 --->>>> sqrt(x2^2 + y2^2)
# x3,y3 --->>>> sqrt(x3^2 + y3^2)
# and so on.
z <- sqrt((x^2)+(y^2))
# > z <- sqrt((x^2)+(y^2))
# > z
#d Find the average of the elements in the z vector. Print the result. (sum(z))/10000000
# > (sum(z))/10000000
# [1] 1.253316
f <- (sum(z))/10000000
#e Square the average and double it. Print the result.
(f^2)*2
# > (f^2)*2
# [1] 3.141604
#f The probability that a standard normal random variable has absolute value
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
greater than 1.96 is about 0.05.
# Let's verify it by experiment: find the proportion of elements in x with absolute value greater than 1.96.
#
You may only use the functions and operators covered in Class 1 and 2. Print the result.
# Hint: For a vector x, the command x>1.96 will give you a list of TRUE and FALSE
corresponding
# to each element.
g<- x>1.96
g
sum(g)
# > g<- x>1.96
# > sum(g)
# [1] 249191
prob <- 249191/10000000
prob
# > prob <- 249191/10000000
# > prob
# [1] 0.0249191
Related Documents
Recommended textbooks for you
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage
Recommended textbooks for you
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:Cengage
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage