Lab MLR and matrix
Lab MLR and matrix
and Matrix
BANA3010 Data driven analytics December 12, 2024
2. Take a vector called "y", make it the column "gpa" of the data set. Find out how many values there
are in the vector. Since we have a copy of the response variable, GPA, stored in y already, let’s remove
the column gpa from the data set.
3. BTW, function which() is a useful function to find the locations within the vector that satisfy some
condition, such as those elements which are greater than zero.
1 which ( y > 0)
4. To create a matrix, we can use the function matrix(). Here we want to convert the current data (now
with 3 columns act, satm, and satv) to a matrix. Use the function as.matrix() to convert the data set
to a matrix X.
5. What’s the dimension of matrix X? You can use the function dim() to find out.
6. You see that the matrix X has 271 rows and 3 columns. Let’s add a column of value 1 (271 of them)
to the front of matrix X. Now matrix X has 271 rows and 4 columns.
7. To create a transpose matrix of matrix X, you can use the function t() : t(X). To see how the function
works, you can make a small matrix and transpose it using the t() function.
8. Lets say we want to build a linear regression model with response variable gpa, stored in vector y, and
predictors: act, satm, and satv stored in X (with a column of ones in front). In the lecture, we showed
that the coefficients of the model β (a vector) can be found by solving system of linear equation:
(X ′ X)β = X ′ y
9. In order to do matrix multiplication in R, you use the operator % ∗ %. Try to multiply X ′ X and X ′ y.
10. To solve for β, you can use the function solve(). The function will solve a system of linear equation:
Ax=b. So in our problem, X’X plays the role of A, X’y is b. Solve the system (X ′ X)β = X ′ y using the
function solve().
11. Finally, fit a linear regression model in R using lm function. Compare the coefficients of the model with
the coefficient vector β that you just found.