1.1. Consider the data set given by x = c(0.725,0.429,-0.372 ,0.863).What value of \(\mu\) minimizes \(\Sigma((x - \mu)ˆ2)\)?
x <- c(0.725,0.429,-0.372 ,0.863)
x
## [1] 0.725 0.429 -0.372 0.863
mean(x)
## [1] 0.41125
x <- c(0.725,0.429,-0.372 ,0.863)
mean(x)
## [1] 0.41125
p<-c(0.25, 0.25, 0.25, 0.25)
xbar<-sum(x*p)
minimum<-sum(p*(x-mean(x))^2)
minimum
## [1] 0.2290782
minimum1<-sum(p*(x-0.41125)^2)
minimum1
## [1] 0.2290782
Using the median for \(\mu\).
median(x)
## [1] 0.577
minimummedian<-sum(p*(x-median(x))^2)
minimummedian
## [1] 0.2565512
As observe in the result above, its the value of the mean that provides the smallest result of $((x - )ˆ2).
Try other value other than the value of the mean and the median, is the result smaller than using the mean in the result of \(\Sigma((x - \mu)ˆ2)\)?
1.2. Reconsider the previous question. Suppose that weights were given, w = c(2, 2, 1, 1) so that we wanted to minimize \(\Sigma(w * (x - mu) ˆ 2)\) for \(\mu\). What value would we obtain?
x <- c(0.725,0.429,-0.372 ,0.863)
w = c(2, 2, 1, 1)
p = c(2/6, 2/6, 1/6, 1/6)
xbar<-sum(x * w) / sum(w)
xbar
## [1] 0.4665
xbarorbs<-c(0.577, 0.577,0.577, 0.577)
minimummean<-sum(p*(x-xbar)^2)
minimummean
## [1] 0.1661253
Using the value of the median
median(x)
## [1] 0.577
minimummedian<-sum(p*(x-median(x))^2)
minimummedian
## [1] 0.1783355
Verify
minimummedian1<-sum(p*(x-xbarorbs)^2)
minimummedian1
## [1] 0.1783355
Using the mean
minimummean<-sum(p*(x-xbar)^2)
minimummean
## [1] 0.1661253
Consider the data set given by x = c(0.18, -1.54, 0.42, 0.95).What is the value of \(\Sigma((x - \mu)ˆ2)\)? (Note the value of \(\mu\) is the one that minimizes the result of \(\Sigma((x - \mu)ˆ2)\).
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)
z<-data.frame(x,y)
# Fit the regression through the origin and get the slope treating y as the outcome and x is the regressor.
fit2 <- lm(z$y~z$x)
fit2
##
## Call:
## lm(formula = z$y ~ z$x)
##
## Coefficients:
## (Intercept) z$x
## 1.567 -1.713
Answer: The expected slope is the coefficient on x, i.e. -1.713.