
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%  Homework # 8  KEY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question # 4.4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% (b)
[vnum Y X1 X2] = textread('streetven.dat', '%f  %f  %f %f');
ssize=size(Y,1);
k=2;
U=ones(ssize,1);
X=[U X1 X2];
beta=(inv(X'*X))*(X'*Y);
SSE=(Y - X*beta)'*(Y - X*beta);
beta;
%'hat_y = -20.3520 + 13.3504 x1 +  243.7145 x2' 

% part d
invC=inv(X'*X);
s2=SSE/(ssize - (k+1));
s=sqrt(s2);
t=beta(2,1)/(s*sqrt(invC(2,2))); 
tp_value = 2*(1- tcdf(abs(t), ssize-(k+1))); 

% part e
alpha =0.05;
df=ssize-(k+1);
tcv= tinv(1-alpha./2, df);
lb= beta(3,1) - tcv.* s.*sqrt(invC(3,3));
ub= beta(3,1) + tcv.* s.*sqrt(invC(3,3));
clear;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part a. The first order model is  E(y)= beta_0 + beta_1 x_1 + beta_2 x_2.
% Part b. hat{y} = -20.3520 + 13.3504 x1 +  243.7145 x2 .
% Part c. beta_0 has no meaning because x1=0 and x2=0 are not in the
% observable range. beta_1 = 13.35 means that the mean annual earnings is
% estimated to increase by $13.35 for each additional year of age, holding
% hours worked per day constant.  beta_2 = 243.714 means that the mean
% annual earnings is estimated to increase by $243.714 for each additional
% hour worked per day, holding year of age constant.
% Part d. To determine if age is a predictor of annual earnings, we test 
% H_0: beta_1 =0 vs H_a : beta_1 ~= 0. Since the observed test statistic 
% t = 1.740 and the p-value=0.1073, H_0 is not rejected. So there is insufficient 
% evidence to indicate that age is a useful predictor of annual income.
% Part e.  The 95% CI for beta_2 is (105.334, 382.095). We are confident
% that for each additional hour worked per day, the mean annual salary will
% increase anywhere from $105.334 to $ 382.095, holding age constant.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question #4.8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%part a and b
[trial  diet  Y X1 X2] = textread('Snowgeese.dat', '%f  %s  %f  %f %f');
ssize=size(Y,1);
k=2;
U=ones(ssize,1);
X=[U X1 X2];
beta=(inv(X'*X))*(X'*Y);
SSE=(Y - X*beta)'*(Y - X*beta);
beta;
% 'hat_y = 12.1804 - 0.0265 x1 - 0.4578 x2' 

% part c
invC=inv(X'*X);
s2=SSE/(ssize - (k+1));
s=sqrt(s2);
t=beta(2,1)/(s*sqrt(invC(2,2)));
alpha=0.01;
df=ssize-(k+1);
tcv=tinv(1-alpha/2, df);
%tp_value = 2*(1- tcdf(abs(t), ssize-(k+1))) 

% part d
alpha =0.01;
df=ssize-(k+1);
tcv= tinv(1-alpha./2, df);
lb= beta(3,1) - tcv.* s.*sqrt(invC(3,3));
ub= beta(3,1) + tcv.* s.*sqrt(invC(3,3));
clear;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part a. \hat{y} = 12.1804 - 0.02654 x1 - 0.45783 x2 .
% Part b. beta_0 = 12.1804 is the estimate of the y-intercept. 
%       beta_1=-0.0265. We estimate that the mean weight change will
%       decrease by 0.0265% for each additional increase of 1% in digestion
%       efficiency, with acid-detergent fibre held constant.
%       beta_2=-0.458 .  We estimate that the mean weight change will
%       decrease by 0.458% for each additional increase of 1% in acid-detergent fibre,
%        with digestion efficiency held constant.
%  Part c.  To determine if digestion efficiency is a useful predictor of
%        weight change, we test H_0: beta_1=0 vs H_a : beta_1 ~=0.
%       Since the observed t=-0.4962 and the rejection region is (-infty,
%       -2.7079)U(2.7079, \infty). H_0 is not rejected. There is
%       insufficient evidence to indicate that digestion efficiency is a
%       useful linear predictor of weight change at alpha=0.01 .
%  Part d. The 99% CI for beta_2 is (-0.8052, -0.1105). We are 99%
%     confident that the change in mean weight change for each unit change in acid-detergent fibre, 
%     holding digestion efficiency constant is between -0.8047 and -0.1109.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #4.12
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

R2=0.7;
n=76;
k=3;
F=(R2/k)/((1-R2)/(n-(k+1)));
alpha=0.05;
df1=k;
df2=n-(k+1);
fcv=finv(1-alpha, df1,df2);
clear;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part a. R^2 = 0.7 .  70% of the sum of squared deviations of the sample
%       Zung depression scores about their mean can be explained by the proposed
%       model.
% Part b.  To determine if the model is useful in predicting the Zung
%      depression scores, we test:
%      H_0: beta_1=beta_2 =beta_3 =0
%      H_a:  at least one beta_i ~=0
%      Since the observed F=56.000 and the F_critical value F(1-0.05, 3,
%      72)=2.7318, H_0 is rejected. There is sufficient evidence to
%      indicate that the model is useful in predicting the Zung depression
%      scores.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question # 4.16
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question ##4.16
% Part a. F=20.91. This is the test statistic for testing the hypotheses:
%    H_0:beta_1=beta_2= ...=beta_27=0 vs 
%    H_a: at least one beta_i ~=0
%    Since the p-value is 0.0001, we reject H_0 and conclude that at least
%    one of the independent variables is useful for predicting price
%    Root MSE = 6544.324. Most of the observed sale prices will fall within
%    2s=2 (Root MSE)=13088.648 units of their respective least squares
%    predicted values.
%    R_Sq=0.8140. It represents that 81.4% of the sample variation of the y
%    values that is explained by the least squares regression model. R_Sq
%     =0.814 implies 81.4% to fit of the model to the data. 
%    Adj R_Sq=0.7751. After adjusting, 77.51% of the sample variation in
%    sale price can be explained by the first order model containing the 27
%    independent variables.
% Part b. The independent variables with beta coefficients that are significant different 
%    from 0 are Lotsize, Gla, Halfbath, Backwood, Fence,
%    Scrporch, Ingrpool, Garage, Updkitch, Remkitch, Cenvacum, Skylites. 
%    No. The t tests on individual parameters in this first order model
%    that indicate the variables are nonsignificant do not necessarily
%    indicate that no relationship exists between that independent variable
%    and the dependent variable. It simply indicates that a linear
%    relationship does not exist. It may be that a higher order
%    relationship exists.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question #4.20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

R2=0.362;
n=31;
k=3;
F=(R2/k)/((1-R2)/(n-(k+1)));
alpha=0.05;
df1=k;
df2=n-(k+1);
fcv=finv(1-alpha, df1,df2);
clear;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #4.20
%Part a.  R_Sq=0.362. So 36.2% of the sample variation of the y
%    values that is explained by the least squares regression model containing 
%    the variables self-esteem score, optimism score, and group cohesion score. 
%    R_Sq =0.362 implies 36.2% to fit of the model to the data. 
% Part b. To test the utility of the model, we test:
%    H_0:beta_1=beta_2=beta_3=0 vs 
%    H_a: at least one beta_i ~=0, i=1,2,3 .
%    Since the observed test statistic F=5.1066 and the
%    F_critical=F(1-alpha, 3, 27)=2.9604, H_0 is rejected. The model is
%    useful at alpha=0.05.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question #4.22
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%part e
n=15;
k=3;
p_value=2*(1- tcdf(0.94, n-(k+1)));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question #4.22
%Part a. hat{y}= 1041.8944 - 13.23762 x1 + 103.30564 x2  + 3.62096 x1 x2 .
%Part b. When x2=10, the equation becomes hat{y}=2074.954 + 22.972 x1.
%       The estimated slope relating  earnings to age is 22.972 when number of 
%       hours worked is 10. 
%Part c. When x1=40, the equation becomes hat{y}=512.374+ 248.146 x2. 
%       The estimated slope relating earnings to hours worked is 248.146
%       when age is 40.
%Part d. To determine if age and hours worked interact, we test 
%       H_0: beta_3 =0 vs H_a: beta_3 ~=0.
%       Since the observed t-statistic  for beta_3 is 0.94 and the p-value 
%       is 0.366, we do not reject the H_0.
%Part e. The p-value found is 0.3674 .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question #4.26
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

R2=0.194;
n=1763;
k=9;
F=(R2/k)/((1-R2)/(n-(k+1)));
alpha=0.05;
df1=k;
df2=n-(k+1);
fcv=finv(1-alpha, df1,df2);
clear;




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Question#4.26
%Part a. beta_1=0.02. The mean level of support for the a military response
%     is estimated to increase by 0.02 for each day increase in level of TV
%     news exposeure, all other variables held constants.
%Part b. To determine if an increase in TV news exposure is associated with
%     an increase in support for military resolution, we test:
%     H_0: beat_1=0 vs H_a: beta_1 > 0
%     Since the p-value =0.015, the H_0 is rejected and the answer is yes. an increase
%     in TV news exposure is associated with an increase in support for military 
%      resolution, all other variables held  constants.
%Part c. To determine if the relationship between support for military
%     resolution and gender depends on political knowledge, we test :
%     H_0: beta_8=0 vs H_a: beta_8 ~= 0
%     Since the p-value is 0.02 < 0.05, the H_0 is rejected and the answer
%     is yes.  the relationship between support for military
%     resolution and gender depends on political knowledge, all other variables 
%     held  constants at alpha =0.05.
%Part d.  To determine if the relationship between support for military
%     resolution and race depends on political knowledge, we test :
%     H_0: beta_9=0 vs H_a: beta_9 ~= 0
%     Since the p-value is 0.08 >  0.05, the H_0 is not rejected and the answer
%     is no. There is insufficient evidence that  the relationship between support for military
%     resolution and race depends on political knowledge, all other variables 
%     held  constants at alpha =0.05.
%Part e. R_Sq=0.194.  So 19.4% of the sample variation of the y
%    values that is explained by the least squares regression model containing 
%    the seven independent variables and the two interaction terms. 
%    R_Sq =0.194 implies 19.4% to fit of the model to the data. 
%Part f. To determine the utility of the model, we test 
%      H_0:beta_1=beta_2=beta_3=beta_4=beta_5=beta_6=beta_7=beta_8=beta_9=0
%      H_a: at least one beta_i ~=0, i=1, ...,9
%      Since the observed F statistic is 46.8820 and the critical value 
%      F(1-alpha, 9, 1753)= 1.8852, H_0 is rejected. the model is useful at
%      alpha =0.05 .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%













