Q1

Consider the data set given below x <- c(0.18, -1.54, 0.42, 0.95) And weights given by w <- c(2, 1, 3, 1) Give the value of ?? that minimizes the least squares equation ???ni=1 wi(xi?????)2

x <- c(0.18, -1.54, 0.42, 0.95)
w <- c(2, 1, 3, 1)

# Give the value of ?? that minimizes the least squares equation ??? [ w_i * (x_i ??? ??)^2 ]

mean(x)     # => 0.0025 (INCORRECT)
## [1] 0.0025
sum(w * x)  # => 1.03 (not an option)
## [1] 1.03
sum(x)      # => .01 (not an option)
## [1] 0.01
mean(w*x)   # => 0.2575 (not an option)
## [1] 0.2575
for (i in c(
    .147,   # 3.717  => minimum
    .0025,  # 3.863
    .3,     # 3.880
    1.077)  # 9.769
    ) { print(sum(w*((x-i)^2))) }
## [1] 3.716543
## [1] 3.862994
## [1] 3.8801
## [1] 9.768983

Q2

Consider the following data set x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42) y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05) Fit the regression through the origin and get the slope treating y as the outcome and x as the regressor. (Hint, do not center the data since we want regression through the origin, not through the means of the data.)

x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)
lm(y~x-1) #Here we need to use command x-1 to omit the intercept
## 
## Call:
## lm(formula = y ~ x - 1)
## 
## Coefficients:
##      x  
## 0.8263

Q3

Do data(mtcars) from the datasets package and fit the regression model with mpg as the outcome and weight as the predictor. Give the slope coefficient.

y<- mtcars$mpg
x<- mtcars$wt
lm(y~x)$coefficients
## (Intercept)           x 
##   37.285126   -5.344472

Q4

Consider data with an outcome (Y) and a predictor (X). The standard deviation of the predictor is one half that of the outcome. The correlation between the two variables is .5. What value would the slope coefficient for the regression model with Y as the outcome and X as the predictor?

Q6

Consider the data given by the following x <- c(8.58, 10.46, 9.01, 9.64, 8.86) What is the value of the first measurement if x were normalized (to have mean 0 and variance 1)?

x <- c(8.58, 10.46, 9.01, 9.64, 8.86)
scale(x)[1]
## [1] -0.9718658

Q7

Consider the following data set (used above as well). What is the intercept for fitting the model with x as the predictor and y as the outcome? x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42) y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)

x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)
lm(y~x)
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##       1.567       -1.713

Q9

Consider the data given by x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42) What value minimizes the sum of the squared distances between these points and itself?

x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
mean(x)
## [1] 0.573