%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Homework #7 KEY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #35
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #35
%Answer: 
%   Part a) positively correlated: the number of students and the number of professors at a univeristy are 
%                positive correlated .
%   Part b) negatively correlated: The CPU speed of a computer and the time to finish a task for this computer 
%                are negatively correlated.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #40
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load namegame2.dat
X = namegame2(:,1);
Y = namegame2(:,2);
Xmean = mean(X);
Ymean = mean(Y);
ssize = size(X, 1);
SS_xx = sum((X-Xmean).^2);
SS_xy = sum((X-Xmean).*(Y-Ymean));
SS_yy = sum((Y-Ymean).^2);
beta1 = SS_xy / SS_xx;
beta0 = Ymean - beta1 * Xmean;
r_sqr = SS_xy^2 / SS_xx / SS_yy;
r = sqrt(r_sqr);
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #40
%Answer: r^2=.0544    r=0.2333
% Beacause r is fairly close to zero, there is a very weak positive linear
% relationship between the proportion of names recalled and position.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #44
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #44
%Answer: 
%   Part a) The only difference is that the prediction interval has an extra term 
% (a "1") beneath the radical. Thus, the prediction interval is wider. Let x_p be 
% a given value of x. The error in estimating the mean value E(y) for a given x_p is 
% the distance between the regression line and the true line of means, (\hat{y}-E(y)). 
% In contrast, the error (y_p - \hat{y}), in predicting some future of y is the sum of 
% two errors- the error of estimating the mean of y, plus the random error that is a 
% component of the value of y to be predicted. 
%   Part b) Since the standard error contains the term (x_p-\bar{x})^2/SS_xx,
%  the further x_p is from, the larger the standard error. The implication is our best 
% confidence inteval (narrowest) will be found when x_p = \bar{x}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #45
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SSE=(59.21-3.4*16.22);
s_sq=(SSE/(20-2));

s=sqrt(s_sq);
alpha=.05;
T=tinv(1-alpha/2,20-2);
s_Beta=(s.*sqrt((1/20)+((2.5-2.5).^2)./4.77));
lb=(10.6-T.*s_Beta);
ub=(10.6+T.*s_Beta);
s_Beta2=(s.*sqrt((1/20)+((2-2.5).^2)./4.77));
lb2=(8.9-T.*s_Beta2);
ub2=(8.9+T.*s_Beta2);
s_Beta3=(s.*sqrt((1/20)+((3-2.5).^2)./4.77));
lb3=(12.3-T.*s_Beta3);
ub3=(12.3+T.*s_Beta3);

s_Beta4=(s.*sqrt(1+(1/20)+((3-2.5).^2)./4.77));
lb4=(12.3-T.*s_Beta4);
ub4=(12.3+T.*s_Beta4);
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #45
%Answer: 
%   Part a) SSE=4.062    s^2=0.225
%   Part b) (10.3768, 10.823) when x=2.5, 95% of the mean values of y are
% expected to be contained in this interval.
%   Part c) (8.5806, 9.2194) when x=2.0, 95% of the mean values of y are
% expected to be contained in this interval.
%   Part d) (11.9806 , 12.6194) when x=3.0. We are 95% confident the mean 
%   value of y when x=3.0 is between 11.981 and 12.619 .
%   Part e) The width of the confidence interval increases as the value of
% x moves away from the value of the mean of x.
%   Part f) The 95% prediction interval for x=3.0 is (11.2521 , 13.3479).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question #50
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load namegame2.dat;
data=namegame2;
load namegame2.dat
X = namegame2(:,1);
Y = namegame2(:,2);
Xmean = mean(X);
Ymean = mean(Y);
ssize = size(X, 1);
SS_xx = sum((X-Xmean).^2);
SS_xy = sum((X-Xmean).*(Y-Ymean));
SS_yy = sum((Y-Ymean).^2);
beta1 = SS_xy / SS_xx;
beta0 = Ymean - beta1 * Xmean;
xp = 5;
y_xp = beta0 + beta1 * xp;
SSE = SS_yy - beta1 * SS_xy;
s = sqrt(SSE / (ssize-2));
alpha = 0.01;
t_cv = tinv(1-alpha/2, ssize-2);
se = t_cv * s * sqrt(1/ssize + (Xmean - xp)^2 / SS_xx);
CI_upper = y_xp + se;
CI_lower = y_xp - se;
see = t_cv * s * sqrt(1 + 1/ssize + (Xmean - xp)^2 / SS_xx);
PI_upper = y_xp + see;
PI_lower = y_xp - see;
clear;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #50
%Answer:
%   Part a) We are 99% confident that the mean recall of all those in the 
%   5th position is in (.6460, 0.7592).
%   Part b) We are 99% confident that the actual recall of a person in the 
%   5th position is in (0.0366, 1.3685). Since the proportion of names 
%  recalled cannot be larger than 1, the actual proportion recalled will be 
%  between  0.0366 and 1.000 .  
%   Part c) The prediction interval is wider and will always be wider than 
% the confidence interval because the standard error is always larger.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #60 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load flour.dat
Y = flour(:, 1);
X = flour(:, 2);
ssize = size(X, 1);

% (a) for beta0 = 0
SSxy = sum(X.*Y);
SSxx = sum(X.^2);
beta1 = SSxy / SSxx;
subplot(2,3,1)
plot(X, Y, 'x');
hold on;
xx = 0:5: max(X);
yy = beta1 * xx;
plot(xx, yy);
ylabel('Weight of Shipment','FontName','Bold Arial', 'FontSize',12);
   xlabel('Number of Bags','FontName','Bold Arial',...
   'FontSize',12);

% (b) for beta_0 ~= 0
Xmean = mean(X);
Ymean = mean(Y);
SSxy = sum((X-Xmean).*(Y-Ymean));
SSxx = sum((X-Xmean).^2);
SSyy = sum((Y-Ymean).^2);
beta1 = SSxy / SSxx;
beta0 = Ymean - beta1 * Xmean;
xx = 0:5: max(X);
yy1 = beta0 + beta1 * xx;
plot(xx, yy1, '--k');
hold off;


% (d) hypothesis test
SSE = SSyy - beta1 * SSxy;
s = sqrt(SSE / (ssize-2));
s_beta0 = s * sqrt(1/ssize + Xmean^2/SSxx);
alpha = 0.10;
t_cv = tinv(1-alpha/2, ssize-2);
t_stat = beta0 / s_beta0;
clear;




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #60
%Answer:
%   Part a) y= 46.3992 Beta_1
%   Part b) y=  478.4433 + 45.1525 Beta_1
%   Part c) The estimated \hat{beta}_0 might be different from 0 due to randomness even if the true value 
% of  beta_0 is known to be zero.
%   Part d) T_critical=1.7709 T=0.9057, thus fail to reject H_0 and Beta_0 
% should not be included in the model.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question #72
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


load helium.dat
X = helium(:,1);
Y = helium(:,2);

% (a) find the least squares estimates
Xmean = mean(X);
Ymean = mean(Y);
ssize = size(X, 1);
SSxx = sum((X-Xmean).^2);
SSxy = sum((X-Xmean).*(Y-Ymean));
SSyy = sum((Y-Ymean).^2);
beta1 = SSxy / SSxx;
beta0 = Ymean - beta1 * Xmean;

% (b) CI for beta_1
SSE = SSyy - beta1 * SSxy;
s = sqrt(SSE / (ssize - 2));
alpha = 0.05;
df = ssize - 2;
tcv = tinv(1-alpha/2, df);
beta1_upper = beta1 + tcv * s / sqrt(SSxx);
beta1_lower = beta1 - tcv * s / sqrt(SSxx);

% (c) coefficient of determination
r_sqr = SSxy ^2 / SSxx / SSyy;

% (d) PI  of  y  at  x=-273
xp = -273;
yp = beta0 + beta1 * xp;
PI = tcv * s * sqrt(1 + 1/ssize + (Xmean - xp)^2 / SSxx);
PI_upper = yp + PI;
PI_lower = yp - PI;

subplot(2,3,2);
plot(X, Y, 'x')
hold on
xx = [-273 -255];
yy = beta0 + beta1 * xx;
plot(xx, yy)
 ylabel('Proportion of Impurity','FontName','Bold Arial', 'FontSize',12);
   xlabel('Temperature (C)','FontName','Bold Arial',...
   'FontSize',12);
hold off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #72
%Answer:
%   Part a) y=  -13.4903 - 0.0528 x
% Beta_0 is the proportion of impurity present in helium when the
% temperature is 0 degrees Celsius.  As temperature increases by 1 degree C, the mean proportion 
% of impurity decreases by 0.0528. 
%   Part b) The 95% CI for beta_1 is (-.0707, -.0350) . Yes, the interval supports the hypothesis that
% temperature contributes information about the proportion of impurity 
% passing through helium because 0 is not in this interval.
%   Part c) r^2=.8538 . This value implies that 85.38% of the total sum of
%   sqares of deviation of the proportion of impurity about their means can
%   be attributed to the linear relationship between proportion of impurity
%   and temperature.
%   Part d) The 95% prediction interval for the percentage of impurity
%   passing through solid helium at -273 degree C is (0.5987, 1.2652).
%   Part e) We see that the size of a CI and a prediction CI increase as
%   the distance |x-\bar{x}| increases. In fact, when x is selected far
%   enough away from \bar{x} so that it falls outside the range of the
%   sample data, it is dangerous to make any inferences about y or E(y).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #74
%%%%%%%%%%%%%%%%%%%%%%%%%%%%


alpha=.01;
n=130;
r=.26;
T_critical=tinv(1-alpha,n-2);
T=((r.*sqrt(n-2))./sqrt(1-r.^2));

%Part b
r=-.21;
T_critical=tinv(1-alpha,n-2);
T=((r.*sqrt(n-2))./sqrt(1-r.^2));

r=.19;
T=((r.*sqrt(n-2))./sqrt(1-r.^2))


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Question #74
%Answer:
%   Part a) To test H_0: rho=0 vs H_a: \rho > 0. We get the T_critical=2.3558 and
% the observed t test statistic T=3.0463.  Therefore,  reject null hypothesis and
% Lobdell and Perlman were correct to conclude that "mother and daughter
% loneliness scores were (postively) significantly correlated at alpha=.01.
%   Part b) |r|=.2039 is the critical cut-off value. So any r greater than 
% this would result in a sample correlations that is large enough to 
% indicate (at alpha=.01) that we should reject H_0 and linear correlation exists between the 
% daughter's loneliness score and the variable. Thus
% the number of friends also has a linear correlation with the daughter's loneliness 
% score at alpha=.01.
%   Part c) It would be dangerous to conclude that a casual relationship
% exists between a mother's loneliness and her daughter's loneliness
% because high correlation does not imply causality.
%   Part d) It is dangerous to conclude that the variables with 
% nonsignificant correlations in the table are unrelated because we were 
% only testing linear correlations. It may be that some of the variables are correleted,
% but not linearly correleted.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%








