#Problem_14_9_2.r

x=c(.34,1.38,-.64,.68,1.40,-.88,-.30, -1.18, .50, -1.75)
y=c(.27,1.34,-.53,.35,1.28,-.98,0.72,-.81,.64,-1.59)
# (a) Fit line y=a + bx using lm() in r
plot(x,y)
lmfit1<-lm(y~x)
summary(lmfit1)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.34954 -0.16556 -0.06363  0.08067  0.87278 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.1081     0.1156   0.935    0.377    
## x             0.8697     0.1133   7.677 5.87e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3654 on 8 degrees of freedom
## Multiple R-squared:  0.8805, Adjusted R-squared:  0.8655 
## F-statistic: 58.94 on 1 and 8 DF,  p-value: 5.867e-05
abline(lmfit1,col='green')
lmfit1$coefficients
## (Intercept)           x 
##   0.1081372   0.8697151
# (b) Fit line x=c+dy using lm() in r

lmfit2<-lm(x~y)
summary(lmfit2)
## 
## Call:
## lm(formula = x ~ y)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.91406 -0.03117  0.07484  0.20963  0.44052 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.1149     0.1250  -0.919    0.385    
## y             1.0124     0.1319   7.677 5.87e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3942 on 8 degrees of freedom
## Multiple R-squared:  0.8805, Adjusted R-squared:  0.8655 
## F-statistic: 58.94 on 1 and 8 DF,  p-value: 5.867e-05
lmfit2$coefficients
## (Intercept)           y 
##  -0.1148545   1.0123846
# For x = b1 + b2y
# we get the y vs x line as
# y=-(b1/b2) + (1/b2)x

abline(a=-lmfit2$coefficients[1]/lmfit2$coefficients[2],
       b=(1/lmfit2$coefficients[2]), col="red")
title(main="Y=a + bx (Green)  X=c+dy (Red)")
abline(h=mean(y)); abline(v=mean(x))
abline(h=mean(y));abline(v=mean(x)) # Plot horizontal/vertical lines at y/x means

# (c). THe lines are not the same. The regression of y on x regresses toward the mean
#     of y (less steep slope) and the regression of x on y regresses toward the mean of 
#     x (which is less steep for x vs y, but more steep for y vs x)