Skills Test Answers - R

Which R function fits a simple linear regression model of x predicting y?

lm(y ~ x)
regress y x
proc reg; model y=x; run;
lm(x ~ y)



In a linear regression model with outcome y and linear predictors x1, x2, and x3, which R code correctly includes an interaction term between x1 and x2?

lm(y ~ x1 + x2 + x3 + interaction(x1, x2))
lm(y ~ x1 + x2 + x3 + x1*x2)
lm(y ~ x1:x2 + x3)
lm(y ~ x1:x3 + x1:x2)



What is an appropriate syntax for a 'for' loop in R to run specific simulation code nnn times?

for(i - nnn) { <simulation code> }
for(i in 1:nnn) { <simulation code> }
for i in 1:nnn: <simulation code>
for(i=1, nnn, 1) { <simulation code> }



Which R function imports a comma separated file "X.csv"?

read.csv("X.csv")
import("X.csv")
import.csv("X.csv")
read("X.csv")



Which R function provides basic descriptive summary statistics for a numeric vector x?

summarize x
summary(x)
summarize(x)
proc contents x;



Given the command: Z <- array(h, dim=c(3,4,2)), dim(Z) stands for:

the entire array as an array
the data vector as it was in h
the array of all zeros
the dimension vector c(3,4,2)



What does the following R code output? seq(2,10, by=2)

1 3 5 7 9
2 4 6 8 10
2
2 10



What's the output for command: print("Hello World")

2
OMG
Hello World
NA



What's the value of x after command: x <- 3 < 4

FALSE
4
TRUE
3



Which R code will sort the vector X <- c(1,5,3,9,7) from largest to smallest?

sort(X)
X[sort(X)]
sort(X, decreasing=T)
order(X)



In the R data set X, what value will missing numeric data be assigned? X <- read.csv("X.csv")

.
NA
"missing"
999



What's the output for command : rep(c(1:2), times = 3)

1 2 1 2 1 2
1 1 1 2 2 2
3 6 9
1:2 1:2 1:2



In order to apply lag operator for the data, you should:

convert the data into vector
convert the data into the time-series object
convert the data into integer
convert the data into matrices



How does one obtain the FIRST element of x when x = 1:9?

x[0]
x$1
x[1]
x$0



Which R function provides a histogram of the numeric vector X?

plot X
plot(X)
hist(X)
plot.hist(X)



Which command is used to test if an object is a time series?

as.ts
if.ts
ts
is.ts


The entities on which R operates are technically known as:

commands
objects
events
mode



What does the following R function output? abs(10 - 3 * 4)

2
0
NA
-2



Which of the following is NOT a valid data import function?

readdata
read.csv
read.table
scan



In the plot( ) function, what option is used to specify that the x-axis displays values from 0 to 1?

xlim="0, 1"
xlim=c(0, 1)
xlab=c(0, 1)
xlimit="0, 1"



What's the output for command is.nan(0/0)

NA
FALSE
-Inf
TRUE



What's the output for command: paste("O", "M", "G", sep = "")?

"O" "M" "G"
"OMG"
"O M G"
"omg"



What's the value of 'ind' after running command : z <- c(1:3,NA); ind <- is.na(z)

FALSE TRUE
FALSE
TRUE
FALSE FALSE FALSE TRUE



What does the option 'cex' do in the following R function? text(0, 1, "Hello", cex=2)

Right justifies the word "Hello"
Increases the size of the word "Hello" by a factor of 2
Prints the word "Hello" in bold font
Surpresses printing of the word "Hello"



What is the output of the following R code? x<-2; y <- c(1:3); paste("The value of x is", x, "and the value of y[x] is", y[x], sep=" ")

"The value of x is 2 and the value of y[x] is 4"
"The value of x is 2 and the value of y[x] is c(1:3)[2]"
"The value of x is 2 and the value of y[x] is 2"
"The value of x is 2 and the value of y[x] is 6"



What's the output for command: seq(from = 1, to = 5, by = 3)

1 3 5
1 2 3 4 5
1 4
1 4 7



What is the value of Y? X <- c(1,2,2,2,3,3,4,4,5,6); Y <- unique(X[which(X<4)])

3
1 2 3 4
1 2 2 2 3 3
1 2 3



In order to decide the inequality between x and y, you can use the command:

x != y
x =~ y
x == y
x =! y



What R function can be used to tabulate values of categorical variable Y (columns) by categorical variable X (rows)?

table(X, Y)
tabulate(X, Y)
table(X*Y)
table(Y ~ X)



Which R function adds a line with slope 1 and intercept 0 into an existing plot of Y versus X?

lines(0, 1)
abline(0, 1)
lty=c(0, 1)
smooth.spline(0, 1)



Which command is correct in order to load excel file into R?

d<-read("c:/ceo.csv",header=TRUE,sep=",",na.string=".")
d<-read.table("c:/ceo.csv",header=TRUE,sep=",",na.string=".")
d<-load.table("c:/ceo.csv",header=TRUE,sep=",",na.string=".")
d<-load("c:/ceo.csv",header=TRUE,sep=",",na.string=".")



What's the output for command: paste0("O", "M", "G")?

"O M G"
"omg"
"O" "M" "G"
"OMG"



Suppose numeric vectors X, Y, and Z are all of the same length. Which R command will create a matrix with rows X, Y, Z?

matrix(rbind(X, Y, Z), ncol=3)
matrix(rbind(X, Y, Z), nrow=3)
matrix(X, Y, Z, nrow=3)
matrix(cbind(X, Y, Z), nrow=3)



What is the output of the following R function? yyy <- c(1, 3, NA); fff <- function(xxx) {mean(xxx)}; fff(yyy)

1.5
NULL
2
NA



Which R function will generate a mean for each row in a numeric matrix X with intermittent missing values?

mean(X[1:length(X[,1]),], na.rm=T)
by(X, 1, mean, na.rm=T)
apply(X, 1, mean, na.rm=T)
apply(X, 1, mean)



What does the following R code output? Y <- c(1,2,3); X <- rep(c(1,length(Y)), times=2); X

1 1
1 3 1 3
1 1 3 3
1 2 1 2



What's the output for command: is.na(0/0)?

NA
TRUE
FALSE
-Inf



What is the output of dim(matrix(1:10, ncol=2))?

2 5
20
5 2
10



Let X be a 3x4 matrix with non-zero values. The result of: > apply(X, 2, mean), is:

equal to the result of: >c(mean(X[,1]), mean(X[,2]), mean(X[,3]))
equal to the result of: >c(mean(X[1,]), mean(X[2,]), mean(X[3,]), mean(X[4,]))
equal to the result of: >c(mean(X[,1]), mean(X[,2]), mean(X[,3]), mean(X[,4]))
equal to the result of: >c(mean(X[1,]), mean(X[2,]), mean(X[3,]))



Which is an INCORRECT command to get an explanation on any specific named function?

??log
?rnorm
ts?
help(lm)



Which of the following is a legal sort command?

sort(c(10, -3,4))
sort(data.frame(x = c(10, -3, 4)))
sort(list(10, -3, 4))
sort(10, -3,4)



Which is NOT a valid R function for obtained the residuals from a simple linear regression model of X predicting Y?

lm(Y ~ X)$residuals - lm(Y ~ X)$fitted.values
residuals(lm(Y ~ X))
Y - lm(Y - X)$fitted.values
lm(Y ~ X)$residuals



What does the following R function output? xxx <- c(1, 1, 2, 2, 3, 3, NA, NA); yyy <- c(rep(0, 4), rep(1, 4)); cor(yyy, xxx)

R squared
A correlation coefficient
An error message
NA



Suppose you need to randomly assign 100 study subjects into one of 5 groups such that there are exactly 20 subjects in each group. Which of the following R functions creates a variable Y that randomly distributes the 100 study subjects into 5 groups of equal size?

Y <- rbinom(100, 5, 0.5)
Y <- sample(c(1, 2, 3, 4, 5), 100, replace = TRUE)
Y <- sample(rep(c(1:5), each=20), size=100)
ytemp <- rnorm(100, 0, 1); Y <- cut(ytemp, breaks=c(-10, -2, -1, 1, 2, 10))



Suppose X is the vector c(1:10). Which R code will swap the values of the 3rd element of X and the 7th element of X?

X[c(3,7)] <- X[c(7,3)]
X[3] <- X[7]; X[7] <- X[3]
X[7] <- X[3]; X[3] <- X[7]
replace(X, c(3, 7), c(7,3)



What's the output for command: is.nan(NA)

-Inf
TRUE
FALSE
NA



Which R command is used to merge two data sets X and Y by the variable "ID" that includes all records from both data sets?

merge(X, Y, by="ID")
merge(X, Y, by="ID", all=T)
combine(X, Y, by="ID")
merge(X, Y, id="ID", all=T)



What is the value of Y? Y <- 10 + 100 & !is.na(0)

True
110
0
False



Which R function can be used to produce a simple scatterplot of a numeric vector Y versus a numeric vector X?

scatter(X, Y)
scatter(Y, X)
plot(X, Y)
plot(Y, X)



What's the output for command: rep(c(1:2), each = 3)

1 2 1 2 1 2
3 6 9
1 1 1 2 2 2
1:2 1:2 1:2



Given the command: Z <- array(h, dim=c(3,4,2)), Z[] with an empty subscript or Z with no subscript stands for:

the array of all zeros
the data vector as it was in h
the entire array as an array
the dimension vector c(3,4,2)



Please fill in the blank of the following commands to extract elements X[1,3], X[2,2] and X[3,1] from a 4 by 5 array X and replace these entries in the array X by zeroes: > x <- array(1:20, dim=c(4,5)) > i <- fill in the blank > x[i] <- 0

array(c(1:3,3:1), dim=c(2,3))
array(c(3:1,1:3), dim=c(3,2))
array(c(1:3,3:1), dim=c(3,2))
array(c(3:1,1:3), dim=c(2,3))



What is the output of matrix(1:10, nrow=2)[2,2]?

2 4 6 8 10
2
3 4
4



Which R function fits a simple linear regression model of x predicting y without an intercept?

regress y x, nointercept
lm(y ~ x, intercept=F)
proc reg; model y=x; intercept=F; run;
lm(y ~ -1 + x)



What's the output of the command : x = c(T, T, NA); all(x, na.rm = TRUE)

TRUE
NA
FALSE
TRUE TRUE FALSE



Which is NOT the right way to quit R program?

quit("yes")
exit()
q()
quit()



What is the result of the command: > A<-array(2:1,dim=(2:2)) > B<-array(2:3,dim=(2:2)) > A %*% B %*% A

[,1] [,2] [1,] 21 14
[,1] [,2] [1,] 7 14
[,1] [,2] [1,] 14 21
[,1] [,2] [1,] 14 7



Which is NOT the parameter of command seq()?

times
length
from
by



The command: > A<-array(3:2,dim=c(2,1)) > B <- t(A) > nrow(B) will generate:

[1] 2
[1] 1
[,1] [1,] 3 [2,] 2
[,1] [,2] [1,] 3 2



What is the result of the command: >array(2:1,dim=(2:2))*array(2:3,dim=(2:2))

[,1] [1,] 7
[,1] [1,] 8
[,1] [,2] [1,] 6 2
[,1] [,2] [1,] 4 3



The command:> state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa") > statef <- factor(state) > incomes <- c(60, 49, 40, 61, 64, 60, 59, 54) > tapply(incomes, statef, mean) generates:

tas sa qld nsw nt wa 60.0 49.0 40.0 62.5 60.0 56.5
tas sa qld nsw nsw nt wa wa 60.0 49.0 40.0 61.0 64.0 60.0 59.0 54.0
nsw nt qld sa tas wa 62.5 60.0 40.0 49.0 60.0 56.5
nsw nsw nt qld sa tas wa wa 61.0 64.0 60.0 40.0 49.0 60.0 54.0 59.0



What is the output of the following calculation? t(c(1, 1)) %*% c(1, 1)

2
1
A 2x2 matrix with all cells equal to 1
4



What is the result of the command: >array(2:1,dim=(2:2))%*%array(2:3,dim=(2:2))

[,1] [1,] 7
[,1] [,2] [1,] 6 2
[,1] [1,] 8
[,1] [,2] [1,] 4 3



What's the output for command: paste("O", "M", "G")

"O" "M" "G"
"OMG"
"omg"
"O M G"



The command: > A<-array(3:2,dim=c(2,1)) > B <- t(A) will generate:

[,1] [1,] 3 [2,] 2
[,1] [,2] [1,] 2 1
[,1] [,2] [1,] 3 2
[,1] [1,] 2 [2,] 1



matrix(1:4, ncol = 2)[1,]

2 4
1 4
1 2
1 3



what's the result from following code a = 2; fun = function(x) {a <<- a + 1;return(x + 1)}; z = fun(3); a + z;

6
7
5
NA



What is the output of: rrr <- 5.45667; sprintf("%1.f", round(rrr, 3))

"5"
5.5
5.457
5



Which class avoids making copies of objects?

R5 (reference class)
s4
s3



What values are displayed for the x-axis limits of the following plot? plot(c(0, 2), c(0, 1), xaxt="n", yaxt="n"); axis(side=2, labels=c("0", "10"), at=c(0,1))

0, 1
No values are displayed
0, 2
0, 10



Which R function outputs the day of the month from X <- "2014-May-15"?

strsplit(X, "-")
substr(X, 10, 11)
date(X, "day")
split(X, "-")[3]



Which R function will generate an integer X from Y <- "9.145", where Y is stored as a text string?

X <- round(Y, 0)
X <- as.integer(Y)
X <- is.integer(Y)
X <- as.numeric(Y)



What is the result of command: labs <- paste(c("X","Y"), 1:10, sep="")?

the row vector ["X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"]
the row vector ["X1" "X2" "X3" "X4" "X5" "X6" "X7" "X8" "X9" "X10"]
the row vector ["Y1" "Y2" "Y3" "Y4" "Y5" "Y6" "Y7" "Y8" "Y9" "Y10"]
the row vector ["X1" "Y1" "X2" "Y2" "X3" "Y3" "X4" "Y4" "X5" "Y5" "X6" "Y6" "X7" "Y7" "X8" "Y8" "X9" "Y9" "X10" "Y10"]



Given x <- c(1:3,NA), the command: (x+1)[(!is.na(x)) & x>0] -> z generates:

the row vector [2 3 4]
the row vector [FALSE FALSE FALSE]
the row vector [TRUE TRUE TRUE]
the row vector [1 2 3]



Let X be a n-dimension vector. Which test for: "no value of X is NA"

!is.na(X)
all(is.na(X))
all(!is.na(X))
!all(is.na(X))



What's right about running following command: setClass("Test", contains = "VIRTUAL"); obj <- new("Test")

A S3 class "Test" is defined
Error produced when create an instance for class "Test"
Error produced when define the class.
A new instance of class "Test" is generated.



If Y is a multi-modal vector of integers, which R function would return all modes of Y (the most frequent value(s) of Y)?

mode(Y)
sort(Y, decreasing=T)[1]
sort(table(Y), decreasing=T)
names(table(Y))[table(Y)==max(table(Y))]



Given the command: Z <- array(h, dim=c(3,4,2)), Z[1:24] stands for:

the entire array as an array
the data vector as it was in h
the dimension vector c(3,4,2)
the array of all zeros



The command > Z <- array(h, dim=c(3,4,2)) would use h to set up 3 by 4 by 2 array in Z. However if h is shorter than 24, its values___________________.

would signal an error about mismatching length
are just the same as the length of h
makes Z an array of all zeros
are recycled from the beginning again to make it up to size 24



R operates on named data structures. The simplest such structure is the numeric vector, which is a single entity consisting of an ordered collection of numbers. To set up a vector named x, consisting of 10.4, 5.6, 3.1, 6.4 and 21.7, which is the INCORRECT command:

assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
(All of these are correct)
c(10.4, 5.6, 3.1, 6.4, 21.7) -> x



What is NOT the right answser about how to get help manual for function 'plot' in a running R session.

help.start(plot)
help(plot)
?"plot"
help("plot")
?plot



Which one is the correct expression regarding outer product of two arrays?

> ab <- a * b
> ab <- outer(a, b, %o%)
> ab <- a %o% b
> ab <- outer(a, b, *)



The command: > fruit <- c(5, 10, 1, 20); > names(fruit) <- c("orange", "banana", "apple", "peach"); > lunch <- fruit[c("apple","orange")]; > lunch; generates:

1 5
1 5 apple orange
apple orange 1 5
apple orange



plot.lm is a function in R, what kind of programming style it is?

R5
n6
s3
s4



What R function can be used to tabulate values of categorical variable Y (columns) by categorical variable X (rows), including missing values?

table(X*Y, na.rm=F)
table(X, Y, useNA="always")
table(X, Y, na.include=T)
tabulate(X, Y, na.omit=F)



Which R code subsets a matrix Y to only those records (rows) where a variable x (in Y) is less than 100?

Y[,which(x<100)]
Y[where(x<100),]
Y[x<=100]
Y[which(x<100)]



Which of the following command only assumes a common continuous distribution?

t.test(A, B)
var.test(A, B)
wilcox.test(A, B)
t.test(A, B, var.equal=TRUE)



Which of the following is NOT one of R's basic vector types?

(All of these are valid)
double
complex
logical
integer



Which of the following statements regarding mixed vector and array arithmetic is true?

Any vector operand shorter than a matrix or array operand generates an error
The expression is scanned from right to left
Any short vector operands are extended by recycling their values until they match the size of any other operands
As long as short vectors and arrays only are encountered, the arrays can have the different dim attribute or an error results



To test the residuals of a regression model X for heteroscedastisity, you can use this command:

heteroscedastisity(X)
bgtest(X)
jarqueberaTest(residual(X))
gqtest(X)



To test the residuals of model X for autocorrelation of first order, you can use this command:

autocorrelationtest(X)
gqtest(X)
bgtest(X)
jarqueberaTest(residual(X))



Which is the INCORRECT way to get the vector [1 1 1 3 3 3 5 5 5]?

x<-seq(1,5,by=2); rep(x, times=3)
x<-seq(1,5,by=2); s<-rep(x, times=3); sort(s)
x<-c(1 1 1 3 3 3 5 5 5)
x<-seq(1,5,by=2); rep(x, each=3)



Which is INCORRECT regarding the command: T=40; x=rnorm(T, 1,4)?

it generates 40 random numbers
it generates 40 random numbers from normal distribution with mean of 1
it generates 40 random numbers from normal distribution with standard deviation of 4
it generates random numbers from 40 different normal distributions



What is the result of command: z <- c(1:3,NA); ind <- is.na(z); z ?

the row vector [1 2 3 NA]
the row vector [FALSE FALSE FALSE TRUE]
the row vector [1 2 3 FALSE]
the row vector [TRUE TRUE TRUE FALSE]



Let h be a numeric vector shorter than 24, the command: dim(h) <- c(3,4,2)

makes an array of all zeros
would signal an error about mismatching length
is recycled from the beginning again to make it up to size 24
is just the same as the length of h