%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This is practice script 2 for the Matlab tutorial% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% rand('state',37); % set arbitrary seed for uniform draws randn('state',37); % set arbitrary seed for normal draws tic; % start stop watch [fid]=fopen('c:\mlab_tut\mlab\logs\script2.txt','w'); if fid==-1; warning('File could not be opened'); break else; disp('File opened successfully'); end; %load data from Excel: load c:\mlab_tut\from_xl.txt; data = from_xl; %rename dataset with a simpler name - optional clear from_xl;% erase original (duplicate) data set - optional % or from STATA: load c:\mlab_tut\from_stata.raw; data = from_stata; clear from_stata; % describe contents of data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %1 runid running observation id %2 orig_solid original solicitor ID %3 amount contributions (2005 dollars) %4 gender solicitor Gender (1=male) %5 race solicitor Race (1=white) %6 height solicitor height (inches) %7 weight solicitor weight (lbs) %8 beauty beauty index %9 spunk sum of personality scores %10 gen_resp observed gender of respondent (1=male) %11 raceresp observed race of respondent(1=white) %12 avg_HHsize average household size (Census block) %13 inc000 Census tract median income (2005 dollars) %14 lottery fundraising package included lottery component % define dependent variable and matrix of explanantory variables y = data(:,3); %":" as a dimension indicator means "all rows" or "all columns"; % here it means "all rows" since it describes the row dimension X = data(:,4:end); % the second ":" means from - to; here from the 4th column to the last % Used in this context "end" indicates the last row, column, or element of a numerical % array n=length(y); k=size(X,2); %Summary statistics: my = mean(y); stdy = std(y); Vy = var(y); Sy = sum(y); out=[my stdy Vy Sy]; fprintf(fid,'Summary stats for y ("amount") \n'); fprintf(fid,'mean\t\tstd\t\tvariance\t\tsum\n'); fprintf(fid,'%6.3f\t%6.3f\t%6.3f\t%6.3f\n',out'); fprintf(fid,'\n'); mX=mean(X,1);%mean for each column; result will be 1 by k % note: add a transpose at the end if you prefer to see this as a k by 1 stdX = std(X,1);%std for each column; result will be 1 by k VX=var(X,1);% variance for each column; result will be 1 by k covX=cov(X,1);% covariance across columns; result will be k by k corrX=corr(X);% sample correlation across columns; result will be k by k sX=sum(X,1);% sum for each column; result will be 1 by k % call OLS function "ols1" and perform remaining regression computations in main % script. bols=ols1(y,X); e=y-X*bols; s2=e'*e/(n-k); Vb=s2*inv(X'*X); se=sqrt(diag(Vb)); t=bols./se; %or: call OLS function "ols2"; [bols,se,t,s2] = ols2(y,X); out=[bols se t]; %combine bols, se, and t vectors into a single matrix fprintf(fid,'Output table for betas \n'); fprintf(fid,'coeff\t\tstd\t\tt-value\n'); fprintf(fid,'%6.3f\t%6.3f\t%6.3f\n',out'); fprintf(fid,'\n'); fprintf(fid,'Squared regression error=\t%6.3f \n',s2); fprintf(fid,'\n'); save c:\mlab_tut\mlab\worksp\script2_stuff y X bols; finish = toc; %this will show run time in seconds fprintf(fid,'Time elapsed in seconds \n\n'); fprintf(fid,'%6.3f\n',finish); st=fclose(fid); if st==0; disp('File closed successfully'); else; warning('Problem with closing file'); end;