You are on page 1of 3

INTRO TO STATISTICAL PROGRAMMING WITH R

FINAL EXAM
SUMMER 2018
DUE DATE: 8/3/18 AT MIDNIGHT

Question 1: Plotting

Read in the attached Titanic dataset, which only consists of the variable Age for the passengers
on the Titanic. Replicate the following plot. Please be sure to submit both your R code and the
resulting plot.

Question 2: Matrices

Please create a matrix that has the values 1, 2, 3, …., 20 in the first row; 2, 3, 4, …, 21 in the
second row; and 3, 4, 5, …, 22 in the third row.

Write the necessary code needed to extract out the values in the sixth row and assign it to a
variable called ‘six_col’.

Write the code that will calculate the sum for 1) each row and 2) each column in the matrix
(hint… a for/while loop is not needed).

1
Question 3: Linear Regression

Run the following code in either R or RStudio.

data <- iris


str(data)

mod <- lm(Sepal.Length ~ Petal.Width + Petal.Length, data)


summary(mod)

res <- mod$resid

a) Which variable(s) is/are the independent variables and which is/are the dependent
variables?

b) How many rows of data can be found in this dataset and how many variables?

c) What is the resulting regression equation?

d) The first residual is 0.214839668. Show how one can manually obtain this residual using
the fact that the residual = observed minus expected.

e) Currently, the Species variable is a factor. If I want to convert it to a character variable,


what code would I need to use to accomplish this?

f) Does the code above reflect Simple Linear Regression or Multiple Linear Regression?
Please explain briefly.

2
Short Answers

1) What is the command to create a vector of the numbers from 1 to 36?

2) What is the sum of TRUE, TRUE, FALSE, FALSE, FALSE, and TRUE? Can this even be done?
If it can, how is it able to be done?

3) What is the difference between getwd() and setwd()?

4) How can we remove everything from our workspace in R?

5) What’s the purpose of the parentheses in the following line of code:


(another_matrix <- matrix(1:12, nrow = 2))?

6) What is the most common use for NULL?

7) What is the command to convert a string to all uppercase? To all lowercase?

8) I have the following string: “314-362-0000”. What code can be used to split the string on
the dashes?

9) What is the easiest loop to master in R?

10) What package is useful for manipulating strings?

You might also like