Computing with Data - Homework 1

September 15, 2013

To begin, load some data into your R session by executing the following.

source("http://www3.nd.edu/~steve/computing_with_data/homework/hmwrk1Data.R")

Warm-up: Vectors of one class can sometimes be coerced into a vector of another class. Look up the help entries for as.character, as.numeric, as.integer. Create vectors of varying types and experiment with the result of trying to coerce it to another type. (Don’t forget logical vectors.)

  1. Given \( x1 \) form a new vector \( y1 \) whose entries are the entries of \( x1 \) with odd index; i.e., \( x1[1] \), \( x1[3] \), ….

  2. Form a vector \( y2 \) such that for each \( j \), \( y2[j]=x1[j] \), if \( x1[j] > 0 \), and \( y2[j] = 0 \), if \( x1[j] \leq 0 \).

  3. Form a vector y3 such that for each \( j \), \( y3[j]=\log(x2[j]) \). Try the same with \( x1 \) and report the result. Note: This is the natural logarithm. log2 is the log base \( 2 \).

  4. Given a discrete random variable \( X \), with values \( X_i \), for \( i \leq n \), let \( p_i \) denote the probability of \( X_i \). Let \( \mu \) denote the mean of \( X \); i.e., \( \mu=\sum_{i\leq n} X_ip_i \).The r-th moment about the mean of \( X \) is \( \mu_r = \sum_{i\leq n}(X_i - \mu)^rp_i \). Consider \( x1 \) as a random variable with distribution \( p1 \). Compute \( \mu_3 \) of \( x1 \). (\( \mu_3 \) is related to the skewness of the distribution.)

  5. Create a vector \( y4 \) whose ith entry is \( 0 \) if \( x1[i]> 0 \) and \( 1 \) if \( x1[i] \leq 0 \).