Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Question 2 The objective of this question is to show how R can be applied in the context of linear regression using matrices. (a) Given

image text in transcribedimage text in transcribed
image text in transcribedimage text in transcribed
Question 2 The objective of this question is to show how R can be applied in the context of linear regression using matrices. (a) Given n pairs of values {(x1, y1), (x2, y2), . .., (In, Un) }, the sample (Pearson) correlation coefficient r is defined by E ( x - x) (yi -4) r = The cor () function can input two vector arguments of the same length and output the correlation coefficient between them. Write a function called my_cor () that computes the correlation between two numeric vectors x and y without the cor (), cov(), var(), or sd() functions. Include a character argument called use that specifies whether to use all observations (use = "everything") or only pairwise complete observations (use = "pairwise. complete. obs" ) by removing a pair (Xi, yi) if either x; or y; is missing. The output of my_cor (x, y) and cor (x, y) should be identical for any numeric vectors x and y. Hint: The cor () function also allows for matrix inputs x and y and computes the correlation matrix between columns of x and columns of y. Your my_cor () function does not need to include this functionality. (b) Assume there is a linear relationship between variables x and y. The least squares regression line is a linear model that predicts y from x. The equation of the regression line is denoted by y = a + bx. The coefficients of the regression line are computed by the formulas b = r- Sx psy and a = y - bx, where a and y and the respective means and sx and sy are the respective standard deviations for the data vectors x and y. Write a function called linreg () that inputs two numeric vectors x and y and outputs a numeric vector of length 2 that corresponds to the coefficients a and b of the least squares regression line that predicts y from x. Include a character argument called use that specifies whether to use all observations (use = "everything") or only pairwise complete observations (use = "pairwise. complete. obs") by removing a pair (xi, yi) if either xi or y; is missing.(C) The heights and weights of six self-identied women are given below. Height (inches) Weight (pounds) 61 104 62 110 63 125 64 141 66 160 68 170 Assume there is a linear relationship between height and weight. We want to t a linear regression model that predicts weight from height. Use your linregO function from (b) to nd the equation of the regression line 9 = a. + bzr. ((0 Write a function called linreg_mat O that inputs two numeric vectors x and y and outputs the expression (X TX )'1XTy, where X is the design matrix given by 1 {1'11 1 1172 X = , , 1 m\" where $1,132,. . . 1%. are the observed data values of the predictor variable m (n is the sample size). Hint 1: Add a column of 1's to the predictor (explanatory) vector to create the design matrix X. Hint 2: Make sure to check that the vector arguments are the same length. (9) Use your 1inreg_mat() function from (d) on the height and weight data in (c). Compare your answer with your results from part (c). (f) Interpret the slope coefcient in the context of the data

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Precalculus

Authors: Michael Sullivan

9th edition

321716835, 321716833, 978-0321716835

Students also viewed these Mathematics questions

Question

Both inkjet and laser printers measure resolution in _ _ .

Answered: 1 week ago

Question

What are the role of supervisors ?

Answered: 1 week ago