output: Homework1_W25.Rmd*: default word_document: null html_document: default df_print: null pdf_document: null editor_options: null chunk_output_type: html_document —

Instructions

Exercise 1: Getting Help in R

This exercise guides you through R’s help system.

The ? symbol can be used to look up documentation for various objects and functions in R.

1(a) Type ?integer into an R console or code chunk. Copy and paste the first example in the “Examples” section into the code chunk below. Be sure to include the comment right above the example!

## Checking to find information on the integer function in R code
?integer
## starting httpd help server ... done
# My code
## Running the code for integer function
## as.integer() truncates:
x <- pi * c(-1:1, 10)
as.integer(x)
## [1] -3  0  3 31

1(b) Code in R can come from the base language, or packages. Sometimes, it may be important to determine whether an object or function comes from base R, or from a package. This can be done with the find() function. Use the find function to look up where the following functions are defined:

?find
# your code

find("sum")
## [1] "package:base"
find('mean')
## [1] "package:base"
find("var")
## [1] "package:stats"
  • sum Answer here: package:base
  • mean Answer here: package:base
  • var Answer here: package:stats

1(c) Some identifiers, like + and if are reserved words in R, meaning R has a special interpretation for those symbols. Trying to look up these items will not work as expected. Instead, you must quote the identifier between quotes (for example, ?“+”). Look up the following items using quotes and state the package that the function comes from. You can also use the function.

# your code

?"+"
?"if"
?"<-"
  • + Answer here: Package: Arithmetic{base}
  • if Answer here: Package: Control{base}
  • <- Answer here: Package: Assign Ops{base}

1(d) The dot product, or inner product, is ubiquitous in statistics, applied math, and physics. Mathematically, it is defined as

\[ x \cdot y = x^{\top}y = \sum_{i=1}^{n} x_{i}y_{i} = x_{1} y_{1} + x_{2} y_{2} + \ldots + x_{n} y_{n} \] for real-valued vectors \(x,y \in R^{n}\) with \(n\)-components.

The code below gives one way to compute the dot product using vectorized operations (try it).

set.seed(1903) # this sets a random number generator (RNG)
x <- rnorm(10) # sample 10 normally distributed numbers
y <- rnorm(10) # ditto

sum(x*y)       #Sum of the two variables "x and y"
## [1] 2.032342

Explain how * operator and the sum function work to compute the quantity. Use the help system if you need it.

Answer here: The ‘’ function is an operator that works to multiply two numbers which is also used in the Dot Product. “” is used to multiply two vectors together. The ’sum’ operator works to use arithmetic method to add vectors into one vector.

The binary operators return vectors containing the result of the element by element operations. If involving a zero-length vector the result has length zero. The operators are + for addition, * for multiplication.

Exercise 2: Sequences of Numbers

2(a) Another operator we did not discuss is :. Look up this operator and discuss what it does. Include information about the resource you used (website, book, etc).

#Answer:
##The ":" operator is used to find integers or real numbers "to" and "from" a given interval. This operator is useful to find data within that given interval.

?':'
##Reference found in 'R Help.
###Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

?: Your answer here: “:” is an operator that generates regular sequences. It starts and ends the interval on a continuous function. It creates an interval for values ‘to’ and ‘from’ values. This operator is used to find integers or real numbers “to” and “from” a given interval. This operator is useful to find data within that given interval.

2(b) Use the operator : and other operators to create a vector with all odd numbers between or equal to 1 and 101.

# your code
1:4
## [1] 1 2 3 4
pi:6 # real
## [1] 3.141593 4.141593 5.141593
for(x in 1:101) #created a variable x from 1 to 101
if(x%%2!=0)     #created a statement that looks for values that have a remainder of 2
print(x)        #told the computer to print these values
## [1] 1
## [1] 3
## [1] 5
## [1] 7
## [1] 9
## [1] 11
## [1] 13
## [1] 15
## [1] 17
## [1] 19
## [1] 21
## [1] 23
## [1] 25
## [1] 27
## [1] 29
## [1] 31
## [1] 33
## [1] 35
## [1] 37
## [1] 39
## [1] 41
## [1] 43
## [1] 45
## [1] 47
## [1] 49
## [1] 51
## [1] 53
## [1] 55
## [1] 57
## [1] 59
## [1] 61
## [1] 63
## [1] 65
## [1] 67
## [1] 69
## [1] 71
## [1] 73
## [1] 75
## [1] 77
## [1] 79
## [1] 81
## [1] 83
## [1] 85
## [1] 87
## [1] 89
## [1] 91
## [1] 93
## [1] 95
## [1] 97
## [1] 99
## [1] 101

Exercise 3: Coercion and Vector Types

3(a) Vectors built using c() must be all of the same type. If given a combination of different types of elements, R will convert all the elements to be of the same type using a series of internal rules. Create a series of vectors that are composed of elements of different types and use the class() function to see how R decides what type of vector to create. You do not need to show intermediate steps or R code here, just fill out your results after the “–>”. The first one is filled out for you.

Write auxiliary code here, if you need it.

#Exercise 3(a) 

r<- "Paul Plecnik"
L<- 10L
m<-TRUE
J<- FALSE
K<- 5.50
I<- 15+20i
p<- 24/12
E<- "Stat107"
#Coding withClass function
class(p)
class(r)
class(m)
class(K)
class(J)
class(E)
class(L)
class(I)
class(a)
vector("character", length= 1L)
vector("numeric",length= 1L)
print(character)
  • integer, double –> double

  • integer, character –> character

  • integer, logical –> logical

  • double, character –> (double)

  • double, logical –> (logical)

  • character, logical –> (Character)

  • integer, double, character –> (integer)

  • integer, double, logical –> (double)

  • integer, character, logical –> (character)

  • double, character, logical –> (logical)

  • double, logical, integer, character –> (integer)

3(b) Describe and comment on patterns that you see in 3(a). Does it look like these conversions are random?

ANSWER HERE: Yes it appears they are random, but the class tells us which type of element we are using for each variable. The randomness is generated through making the vectors different and unique. There is no pattern, but there are certain elements that are common.

Exercise 4: Measures of Center

When analyzing data, it is very useful to find the mean and standard deviation. For the following exercise, you will create code to calculate the mean. For observations \(x_1, x_2, \ldots, x_n\), we define the sample mean as,

\[\bar{x} = \frac{x_1 + x_2 + x_3 + \ldots + x_n}{n}. \]

5(a) First use the function mean to calculate the mean of the following vector.

x_vec <- c(1,2,3,4,5,6,7,8,9,10)

# Your code here:
mean(x_vec)       #Created a mean of x_vector 
## [1] 5.5

5(b) Create code to calculate the mean of the vector x_vec. (Hint: use the functions sum and length)

# Your code here:
sum(x_vec)/length(x_vec)
## [1] 5.5

Exercise 5: Measures of Spread

In this exercise, you will use functions to calculate the standard deviation of a vector of data. Recall that for a vector of data \(x = (x_1, x_2,x_3,\ldots, x_n)\) of \(n\) observations with mean \(\bar{x}\), the standard deviation is given by:

\[ \sigma = \sqrt{\frac{(x_1 - \bar{x})^2+(x_2 - \bar{x})^2+\ldots + (x_n - \bar{x})^2 }{n - 1} } \]

5(a) Use the function sd to calculate the standard deviation of the given vector.

x <- c(1,2,3,4,5,6,7,8,9,10,11)

# Your code here:
  sd(x)
## [1] 3.316625

5(b) Now, lets try to calculate the standard deviation “by hand”. To do this, first create code to calculate the deviations of the entries of x away from their mean \(\bar{x}\), given by \((x_1 - \bar{x}, x_2 - \bar{x},\ldots,x_n-\bar{x})\). Store them in the vector devs (Hint: it can be done in 1 line,, using the function mean)

# your code
mean(x)
## [1] 6

5(c) Now, calculate the square sum of deviations, given by \(D = (x_1-\bar{x})^2 + (x_2-\bar{x})^2 + \ldots + (x_1-\bar{x})^2\). (Hint: it can be done in one line with the operator ^2 and the sum function).

# your code
sum(x^2)
## [1] 506

5(d) Now, divide by the number of entries (minus 1), and take square root to find the standard deviation. Is it the same as the one calculated with sd? (Hint: can be done in one line using the sqrt and lenght functions)

# your code

sqrt(x)/(length(x))
##  [1] 0.09090909 0.12856487 0.15745916 0.18181818 0.20327891 0.22268089
##  [7] 0.24052285 0.25712974 0.27272727 0.28747979 0.30151134