To Lecture Notes

CSC 423 -- 5/22/17

Review Exercises

  1. What happens when you call the readLines function like this:

    Ans: The readLines function reads lines from the keyboard. It reads from the Console keyboard like this:
    terminate the input with Control-Z for Windows; Control-D for Mac and Unix.  For example:
    Terminating the keyboard input with Control-Z does not work in RStudio, but you can specify the number of lines to read like this:
    Ther readLines function also reads from a file:
  2. What happens when you read a data frame like this?
    Ans: The header is treated as data and the default variables V1, V2, V3, etc. are used for the columns. You can add column names like this:
  3. How does the R table function work? Ans: Here are two examples.
  4. How does the aggregate function differ from the table function?
     
  5. Consider the scatterplot of the dataframe df created by
    What is wrong?
    Ans: Factors are stored as numeric values. "7", "3", "9", "2" are stored as 3, 2, 4, 1, which are the values that are plotted. This is how to actually plot the character values:
    Of course if the values 7, 3, 9, 2 were entered in column 2 of the data frame instead, these values would not be stored as factors, so the dataframe could be plotted as
    or, even easier,
  6. Why is the multivariate normal distribution important?
    Ans: The multinormal distribution occurs frequently in nature. It is also the theoretical basis for linear and quadratic discriminant analysis, which is an important supervised machine learning technique. Perform discriminant analysis in SAS with proc discrim and in R with the lda (linear discriminant analysis) and qda (quadratic discriminant analysis) functions in the MASS package.
     
  7. What is an S3 class? How are S3 objects created?
    Ans: An S3 class is a list with a class attribute that contains the name of the class. To create an S3 object, create a list with the object's data fields, then set the class attribute to the name of the class. An S3 object is usually created with a constructor.  Here is the Kid constructor:
  8. What are generic functions? How are they used in S3 classes?
    Ans: The generic funtion haveBirthday is defined like this:
    This generic function determines the class of the object that is calling it and then calls the specific haveBirthday method for that class, in this case haveBirthday.Kid.
     
  9. Use R to compute the 0.63 quartile for the vector
    using methods 1 and 4. Verify your results by hand.
    Ans: First sort the vector:
    Compute the 0.67 quartile = Q(0.67) using Method 1:
    We have
     
    Index: 1 2 3 4 5
    Quantile: 0.2 0.4 0.6 0.8 1.0
    Value: x1=19 x2=21 x3=45 x4=76 x5=111

    To find the 0.67 quartile, note that 0.67 is between quantile 0.6 and 0.8 in the table--we round up to 0.8, so Q(0.67) = x4 = 76.
     
    Compute Q(0.67) using Method 4:
    Using the table above we see that Q(0.6) = x3 = 45 and Q(0.8) = x4 = 76. Use linear interpolation to find Q(0.67)
     
    interpolation diagram
     
    We use similar triangles:
     

    Check with R:
    Here is the R script that draws the interpolation diagram.
     
    See the Quantile Computations document for the details of computing quantiles of types 1 to 9.
     
  10. Write an R function that computes the quantile using the R type 4 calculation. Ans:
  11. What do eight these R functions do? Which SAS functions correspond to them:
     
      R SAS
      Normal Uniform Normal Uniform
    Density (PDF) dnorm dunif pdf("normal", ...) pdf("uniform", ...)
    Cumulative (CDF) pnorm punif cdf("normal", ...) cdf("uniform", ...)
    Quantile (IDF) qnorm qunif quantile("normal", ...) quantile("uniform", ...)
    Random Variable (RV) rnorm runif rand("normal", ...) rand("uniform", ...)

 

Random Number Generators

Practice Problems

 

Tests for Uniform Random Number Generators

Reading from SAS sas7bdat Files

 

Reading from JSON Files

 

Parallel R Packages

 

Alternatives to R Base Graphics

 

Using R and SAS to Create Maps

 

Using the RStudio Debugger

 

Review for Final