0% found this document useful (0 votes)
64 views5 pages

CS2610 Final Exam: If Is - Nan Print

The document describes a final exam for a CS2610 course. It includes 6 questions covering common uses of numeric values like NaN, Inf, factors in R, writing functions to analyze vectors and matrices, and using linear regression to calculate least squares solutions. Question 1 defines NaN, Inf, NA, null, numeric, and character values. Question 2 returns the smallest and largest 3 numbers in a vector. Question 3 covers factors, their uses, and generating random data frames. Question 4 returns indices where values in a vector fall in a given range. Question 5 multiplies two matrices with checks on dimensions. Question 6 calculates least squares solutions using linear regression.

Uploaded by

Aneudy M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views5 pages

CS2610 Final Exam: If Is - Nan Print

The document describes a final exam for a CS2610 course. It includes 6 questions covering common uses of numeric values like NaN, Inf, factors in R, writing functions to analyze vectors and matrices, and using linear regression to calculate least squares solutions. Question 1 defines NaN, Inf, NA, null, numeric, and character values. Question 2 returns the smallest and largest 3 numbers in a vector. Question 3 covers factors, their uses, and generating random data frames. Question 4 returns indices where values in a vector fall in a given range. Question 5 multiplies two matrices with checks on dimensions. Question 6 calculates least squares solutions using linear regression.

Uploaded by

Aneudy M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS2610 Final Exam

1. Describe the most common use of each of the following and show an if statement to test for it in a
variable x. (14 points)
• nan: not a number, most common is to indicate is not a number.
if (is.nan(x)) {print("x is NaN")}

## [1] "x is NaN"


• inf: infinite, most common is when a non zero number is divided by zero.
if (is.infinite(x)) {print("x is infinite")}

## [1] "x is infinite"


• NA: not available, most common use to indicate a missing value.
if (is.na(x)){print("x is not available")}

## [1] "x is not available"


• null: is often returned by expressions and functions whose value is undefined.
if (is.null(x)){print("x is null")}

## [1] "x is null"


• 0: is numeric, most common use is with numbers.
if (is.numeric(x)){print("x is numeric")}

## [1] "x is numeric"


• “0”: is a character, most common use with Strings.
if (is.character(x)){print("x is character")}

## [1] "x is character"

2. Show a function that returns two vectors v1 and v2 in a list that are the smallest and largest 3 numerics
in the vector x. Check that the x is at least length 3 and numeric. If it is not return an NA. (17 points)
q2 <- function(x) {
if (length(x) < 3 || !is.numeric(x)) { return(NA) }
return(list('v1' = sort(x)[1:3], 'v2' = sort(x,decreasing = TRUE)[1:3]))
}
q2(c(1,6,9,7,2,11,3))

## $v1
## [1] 1 2 3
##
## $v2
## [1] 11 9 7
q2(c(1,2))

## [1] NA

1
3. Factor questions: (17 points)
• What is a factor in a data frame? A factor is a categorical variable. (2 points)
• What is the main use for factors? Factors are used to represent categorical data. Factors can be ordered
or unordered and are an important class for statistical analysis and for plotting.Factors provides levels
for categorical variables (2 points)
• Show how to read in a data frame “dataSet” without treating any columns as factorsl. (2 points)
dataSet = read.table("nfl.csv", sep="," ,header = TRUE)
dataSet = data.frame(dataSet, stringsAsFactors = FALSE)

• For a data frame “dataSet”, show how to check that the name of the columns match the names in a
list of strings called “colNames” by writing a function that returns T or F depending on weather they
match. (6 points)
checkNames <- function(dataSet, colNames) { #identical()
if (sum(colnames(dataSet) == colNames) == length(colnames(dataSet)) ) {
return(TRUE)
}else {
return(FALSE)
}
}
colNames = c("y" , "x1" ,"x2" ,"x3", "x4" ,"x5" ,"x6" ,"x7" ,"x8" ,"x9")
a = c("a", "b")
checkNames(dataSet,colNames)

## [1] TRUE
checkNames(dataSet,a)

## [1] FALSE
• Generate a dataFrame with n rows and three columns containing random normal samples with mean 2
and sd 7. Name the columns “col1”, “col2” and “col3” in your data frame.(5 points)
generateDataFrame <- function(n, mean = 2, sd = 7) {
col1 <- rnorm(n, mean, sd)
col2 <- rnorm(n, mean, sd)
col3 <- rnorm(n, mean, sd)
df <- data.frame(col1 = col1, col2 = col2, col3 = col3)
return(df)
}
generateDataFrame(3)

## col1 col2 col3


## 1 -3.9711384 -2.674908 3.6041310
## 2 -0.5914643 9.308356 2.7518353
## 3 -0.2898936 3.427241 -0.2392262

4. Write a function to return the indices of vector of a vector x where 2 ≤ xi < 5. Do not use any loops.
(17 points)
getIndices <- function(x) {
if (!is.vector(x) || !is.numeric(x) ) {
return(NA)
}else {

2
return(which(x >= 2 & x < 5))
}
}
x = c(1,6,"9")
getIndices(x)

## [1] NA
x = c(1:9)
getIndices(x)

## [1] 2 3 4

5. Write a function that returns the product of numeric matrices a and b using two nested for loops. Check
that a and b are numeric matrices of the correct dimensions to multiply or return NA. (18 points)
• Function that returns the product of numeric matrices: (12 points)
multiplyMatrix <- function(a, b) {

if (!is.matrix(a) || !is.numeric(a)) {
return(NA)
}

if (!is.matrix(b) || !is.numeric(b)) {
return(NA)
}

if (ncol(a) != nrow(b)) {
return(NA)
}

c = matrix(NA, nrow = nrow(a), ncol = ncol(b))

row = seq_along(b[1,])
col = seq_along(a[,1])
for (i in row) {
for (j in col) {
c[j, i] <- sum(a[j,] * b[,i])
}
}
return(c)
}
a = matrix(c(1:4), 2 , 2)
a

## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
b = matrix(c(1:6), 2, 3)
b

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


## [1,] 1 3 5
## [2,] 2 4 6

3
multiplyMatrix(a,b)

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


## [1,] 7 15 23
## [2,] 10 22 34
• Show how to multiply a and b with the matrix multiply operator. (2 points)
a %*% b

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


## [1,] 7 15 23
## [2,] 10 22 34
• What does the multiply operator do when the dimensions are not correct to a usual matrix multiply?
(2 points)
c = matrix(c(1:6),3,2)
c

## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
a %*% c

## Error in a %*% c: non-conformable arguments


• What does the matrix multiply operator do when there is a single NA entry at location i,j in a and all
other entries are usual numerics?(2 points)
d = matrix(c(1:5,NA), 2, 3)
d

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


## [1,] 1 3 5
## [2,] 2 4 NA
a %*% d

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


## [1,] 7 15 NA
## [2,] 10 22 NA

6. Given n by m numeric matrix X and n by 1 numeric vector y, show code to use the function lm and
print only the least squares solution b to y = Xb + e, and nothing else. (17 points)
• There are 3 ways to solve b:
vec = rnorm(10,13,1.5)
x = matrix(vec,5,2)
y = c(11:15)
getBetas1 <- function(x, y) {
if (!is.matrix(x) | !is.vector(y)) {
return(NA)
}
print(coef(lm(y~x)))

4
}
getBetas1(x,y)

## (Intercept) x1 x2
## 10.7913138 -0.4761347 0.6193482
getBetas2 <- function(x, y) {
if (!is.matrix(x) | !is.vector(y)) {
return(NA)
}
x = data.frame(x,y)
print(coef(lm(y~.,x)))

}
getBetas2(x,y)

## (Intercept) X1 X2
## 10.7913138 -0.4761347 0.6193482
getBetas3 <- function(x, y) {
if (!is.matrix(x) | !is.vector(y)) {
return(NA)
}
x = matrix(c(rep(1,nrow(x)),x),nrow(x),ncol(x)+1)
b = c(solve(t(x)%*%x)%*%(t(x)%*%y))
names(b) = c("(Intercept)", "X1", "X2" )
print(b)

}
getBetas3(x,y)

## (Intercept) X1 X2
## 10.7913138 -0.4761347 0.6193482

You might also like