Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Syntax and class-typing. a. For each of the following commands, either explain why they should be errors, or explain the non-erroneous result. vector1 =
1. Syntax and class-typing. a. For each of the following commands, either explain why they should be errors, or explain the non-erroneous result. vector1 = c("5", "12", "7", "42") max(vectori) sort (vectori) sum(vectori) b. For the next series of commands, either explain their results, or why they should produce errors. vector2 = c("5",9,12) vector2[2] + vector2[3] dataframe 3 = data.frame(vl="5",v2=10,v3-12) dataframe 3[1,1] + dataframe3[1,3] dataframe 3[1,2] + dataframe 3[1,3] list4 = list(vl="6", v2=42, v3="49", v4=126, v5 = "25") list4[[3]]+list4[[4]] list4[3 ]+list4[4] 2. Data frames Rincludes a number of pre-specified data objects as part of its default installation. We will load and manipulate one of these, a data frame of 93 cars with model year 1993. Begin by ensuring that you can load this data with the commands library (MASS) data (Cars93) Begin by examining the data frame with the command View (Cars93) to understand the underlying object. You must use functions and other commands to extract elements for the questions below; not a visual inspection using View ( Cars93). a. What are the column names of the data frame Cars93 ? What are the number of rows in this data frame? b. How many of these cars are US-made? Look at the column named Origin. c. What is the mean price of a US-made car? A non-US-made car? (What are the units here?) d. What is the biggest difference between highway MPG and city MPG? Which make of car achieves this difference? e. How many different cars have no airbags? Of these, how many are US-made, and how many are non-US-made? f. Assuming that these cars are exactly as fuel efficient as this table indicates, find the cars that have the maximum, minimum and median distance travellable for highway driving. To locate these cars, you will need at least two columns (why?). 3. Lists The singular value decomposition (SVD) is something that you likely encountered in your linear algebra class. Though it may have seemed abstract when you learned it, the SVD actually plays a really big role in much of advanced statistical modeling. When you learn things like ridge regression, principal components analysis, smoothing splines, etc.- all of these things (and more!) can be understood from the perspective of the SVD. a. Compute a singular value decomposition of the matrix mat = matrix(rnorm(10), ncol=2) , using the syd() function, and store the result in a variable called mat.svd. What is the class of mat.svd ? What are the names of its components? b. Mathematically, if we denote mat by a matrix M , then its SVD can be written as M = UDV", where D is a diagonal matrix, containing what are called the singular values of M. Using the components of svd.mat , form the three-term matrix product UDV?. (Hint: you will need to use the diag() function in order to turn a vector into a diagonal matrix, and the t() function in order to perform the matrix transpose operation.) Check that this matrix product is actually equal to the original matrix mat . (Hint: to check whether all entries of one matrix are equal to the other, you can use all.equal() .) c. Again denoting mat as M, mathematically, compute the cross-product matrix M'M, and call this mat.cross . Now compute an eigendecomposition of mat.cross , using the eigen() function, and store the results in a variable called mat.eigen . What is the class of mat.eigen ? What are the names of its components? d. Check that the squared singular values of mat are equal to the eigenvalues of mat.cross . Bonus: can you explain why this is the case? (This requires a bit of advanced linear algebra, so don't worry about it if you don't see why this is true...)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started