0% found this document useful (0 votes)
13 views2 pages

Lab MLR and matrix

Uploaded by

lthykhue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views2 pages

Lab MLR and matrix

Uploaded by

lthykhue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Lab Practice: Multiple linear regression

and Matrix
BANA3010 Data driven analytics December 12, 2024

Lab Practice Submission Instructions:


• This is an individual lab practice and will typically be assigned in the laboratory (computer lab). You
can use your personal computer but the practical exams will be performed with a lab computer.
• Your program should work correctly on all inputs. If there are any specifications about how the program
should be written (or how the output should appear), those specifications should be followed.
• Your code and functions/modules should be appropriately commented. However, try to avoid making
your code overly busy (e.g., include a comment on every line).
• Variables and functions should have meaningful names, and code should be organized into function-
s/methods where appropriate.
• Academic honesty is required in all work you submit to be graded. You MUST NOT copy or share your
code with other students to avoid plagiarism issues.
• Use the template provided to prepare your solutions.
• You should upload your .R file(s) to the Canvas before the end of the laboratory session unless the
instructor gave a specified deadline.
• Submit separate .R file for each Lab problem with the following naming format: Lab4_Q1.R. Failure
to submit a .R file for lab or assignment will result in a 0.
• Late submission of lab practice without an approved extension is NOT allowed.

Lab Practice MLR Page 1


1. Lets read the data set "GPA.txt" in as a data frame in R. The columns of the data set are: act: ACT
score, gpa: college GPA score, satm: SAT Math score, satv: SAT verbal score.

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

, where X’ is the transpose of matrix X.

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.

Lab Practice MLR Page 2

You might also like