1 (2 pts)

Make the following three vectors:

#  -1.00 -0.75 -0.50 -0.25  0.00  0.25  0.50  0.75  1.00
vector_1 <-seq(-1,1, .25)
#  "a" "a" "b" "b" "c" "c" "d" "d" "e" "e" "f" "f" "g" "g"

[YOUR ANSWER..]

#  "1_a" "2_b" "3_a" "4_b" "5_a" "6_b" "7_a" "8_b"

[YOUR ANSWER..]

2

a (1 pt)

Make a matrix named that has 5 columns and 6 rows, and fill it with random samples from the normal distribution (mean = 3, standard deviation = 1).

myMatrix <- matrix(NA, 6, 5)

for (i in 1:nrow(myMatrix)) {
  myMatrix[i,] <- rnorm(5, 3,1)
}

myMatrix
##          [,1]     [,2]     [,3]      [,4]     [,5]
## [1,] 2.187722 2.717107 5.411908 0.9039204 3.047016
## [2,] 2.864023 3.661594 1.349833 3.5902652 1.913081
## [3,] 3.359563 2.931281 2.380193 3.6101982 1.732351
## [4,] 4.045473 3.532376 3.830165 4.1213507 3.191283
## [5,] 2.795564 2.376155 1.233600 4.4541617 2.898028
## [6,] 3.481573 3.956819 3.247257 1.9437231 3.479346

b (1 pt)

Make a list with two items. The first item is a vector with all the of the matrix . The second item is a vector with all the of the matrix .

means <- apply(myMatrix, 2, mean)
medians <-apply(myMatrix, 1, median)

myList <- list(means, medians)

3

Load the dataset from the library MASS.

What are the standard deviations of horse power, for each level of , for each level of (e.g., the sd of horse power for cars with \(= 0\) and \(= 1\), etc.)?

a (1 pt)

Do this with an implicit loop.

library(MASS)
data("mtcars")
mtcars
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
sd_vs <- tapply(mtcars$hp, mtcars$vs==1, sd)
sd_am <- tapply(mtcars$hp, mtcars$am==1, sd)

sd_vs
##    FALSE     TRUE 
## 60.28150 24.42447
sd_am
##    FALSE     TRUE 
## 53.90820 84.06232

b (1 pt)

Do this with an explicit loop.

mySubset_vs_1 <- numeric()
mySubset_vs_0 <- numeric()
mySubset_am_1 <- numeric()
mySubset_am_2 <- numeric()

thisSD_vs_1 <- numeric()
thisSD_vs_0 <- numeric()
thisSD_am_1 <- numeric()
thisSD_am_0 <- numeric()


# for (i in 1:nrow(mtcars)) {
#   mySubset_vs_1[i] <- subset(myData$hp, mtcars$vs == 1[i])
#   mySubset_vs_0[i] <- subset(myData$hp, mtcars$vs == 0[i])
#   mySubset_am_1[i] <- subset(myData$hp, mtcars$am == 1[i])
#   mySubset_am_2[i] <- subset(myData$hp, mtcars$am == 0[i])
#   
#   thisSd_vs_1 <- sd(mySubset_vs_1)
#   thisSd_vs_0 <- sd(mySubset_vs_0)
#   thisSd_am_1 <- sd(mySubset_am_1)
#   thisSd_am_0 <- sd(mySubset_am_0)
# }

4

a (1 pt)

Write a function called numberFinder. This function takes in a single numeric argument, and the function keeps sampling a single random integer between 1 and 100, until it finds the specified number. The function then returns that number. (tip: first write the main code, and only write a function around it afterwards.)

x <- sample(100,1)

myFunction <- function(x){ if (sample[sample==99]) { print(“Hoera”) }else if(sample[sample==!99]) { print(“Boo”) } }

myFunction(x) #### b (1 pt) Adjust your code from question 4a, such that your function gives an error message if the specified argument is not numeric. If you did not succeed in 4a, simply write a function that gives an error message if the specified argument is not numeric.

[YOUR ANSWER..]

c (1 pt)

Adjust your code from question 4a, such that your function keeps track of the number of samples it has taken, and then returns a list with two items: 1) the number that was found, 2) the number of samples it took.

[YOUR ANSWER..]

5 (2 pts)

Make the plot shown in the pdf. Use the following code to generate x1, x2 and y (tip: the green line is based on the linear regression model that predict y based on x1)

set.seed(21)
x1 <- rnorm(100) # Openness
x2 <- rnorm(100, 1, 3) 
y <- x1 + x2 + rnorm(100, 0, .1) # Agreeableness
plot(y, main= "Association O/A", ylab = "Agreeableness Score", xlab = "Openness Score", pch=1, col= "orange", bty= "n", ylim = c(-10,10), xlim= c(-3,3))
points(y, col="orange")
abline(0,1, col="green")