1. Consider the dataset given by x=c(0.725,0.429,-0.372 ,0.863). What value of mu minimizes sum((x - mu)ˆ2)?
x <- c(0.725, 0.429, -0.372, 0.863);
mean(x)
## [1] 0.41125
  1. Reconsider the previous question. Suppose that weights were given, w = c(2, 2, 1, 1) so that we wanted to minimize sum(w * (x - mu) ˆ 2) for mu. What value would we obtain?
w <-c(2,2,1,1)
sum(x*w)/sum(w)
## [1] 0.4665
library(UsingR)
## Warning: package 'UsingR' was built under R version 4.2.3
## Loading required package: MASS
## Loading required package: HistData
## Warning: package 'HistData' was built under R version 4.2.3
## Loading required package: Hmisc
## Warning: package 'Hmisc' was built under R version 4.2.3
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
data(Galton)
head(Galton)
##   parent child
## 1   70.5  61.7
## 2   68.5  61.7
## 3   65.5  61.7
## 4   64.5  61.7
## 5   64.0  61.7
## 6   67.5  62.2
y=Galton$parent
x_1=Galton$child
yc= y-mean(y)
x_1c=x_1-mean(x_1)
sum(x_1c*yc)/(sum(x_1c^2))
## [1] 0.3256475
lm(yc~x_1c-1)
## 
## Call:
## lm(formula = yc ~ x_1c - 1)
## 
## Coefficients:
##   x_1c  
## 0.3256

linear model with y not centered but the outcome and x as predictor

lm(y~x_1)
## 
## Call:
## lm(formula = y ~ x_1)
## 
## Coefficients:
## (Intercept)          x_1  
##     46.1353       0.3256