Question: use R as coding language Question 4 The objective of this question is to show how R can be applied in the context of linear

use R as coding language

Question 4 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 {(21, y1), (12, y2), ..., (In, Un)}, the sample (Pearson) correlation coefficient r is defined by E (x - 2) (3 -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 (Ti, 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 + br. The coefficients of the regression line are computed by the formulas b= r and a = y - bx, where a and y and the respective means and sr 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. 3 (c) The heights and weights of six self-identified 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 fit a linear regression model that predicts weight from height. Use your linreg () function from (b) to find the equation of the regression line y = a + bx. (d) Write a function called linreg_mat () that inputs two numeric vectors x and y and outputs the expression (x"X)-1Xy, where X is the design matrix given by X1 X = . . . . . . where T1, T2, . .., In are the observed data values of the predictor variable r (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. (e) Use your linreg_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 coefficient in the context of the data
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
