⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hbmreg1.gss

📁 gauss 离散型计量估计源代码,直接下载下来就可以使用
💻 GSS
📖 第 1 页 / 共 2 页
字号:
*		
*	OUTPUT
*		parmat	= updated rankx x mvar coefficient matrix
*		var		= updated variance
*		vari		= updated inverse of sigma
*
*	Calling Statement:
{parmat, var, vari} = getmulreg(yd,xd,xdtxd,parmat,var,vari,v0i,v0iu0,f0n,g0i);
****************************************************************
*/
PROC (3) = getmulreg(yd,xd,xdtxd,parmat,var,vari,v0i,v0iu0,f0n,g0i);
local vb12, ubn, par, pdim, resid, gni, gn, rp, cp;

	rp		= rows(parmat);
	cp		= cols(parmat);
	pdim	= rp*cp;

	/*
	***********************************************************
	* Generate parmat from N_{rp x cp}(M,v)
	* par = vecr(parmat)
	* par is N(u,V) whee u = vec(M');
	*	V = (Xd'Xd.*.Var^{-1} + V_0^{-1})^{-1}
	*	u = V*( (Xd'.*.Var^{-1})*vec(Yd') + V_0^{-1}u_0 )
	*************************************************************
	*/
 
	vb12	= chol(xdtxd.*.vari + v0i);
	ubn		= ( (xd').*.vari )*vecr(yd) + v0iu0;
	par		= cholsol(ubn + vb12'rndn(pdim,1), vb12);
	parmat	= reshape(par,rp,cp);

	/*
	*********************************************************
	* Generate Var
	* 	Var^{-1} is Wishart df = f0n, scale matrix = gn
	*********************************************************
	*/
	resid 			= yd - xd*parmat;
	gni				= g0i + resid'resid;
	gn  			= invpd(gni);
	{vari, var} 	= wishart(cp,f0n,gn);

retp(parmat,var,vari);
endp;


/*
*****************************************************************************************************
*  OUTPUTANAL
*	Does analysis of output and save some results 
****************************************************************************************************
*/
PROC (0) = outputanal;
format 10,5;
local  bout, sout, ebeta, sbeta, cb, rmse, fmtn1, fmtn2, fmts1, fmts2, a,b,
betat, sigmat, thetat, lambdat;

if flagtrue == 1;		@ It was a simulation @
	load betat = betat;
	load sigmat = sigmat;
	load thetat = thetat;
	load lambdat = lambdat;
endif;

let fmtn1[1,3]	= "*.*lf" 10 5;			@ Format for printing numeric variable 		@
let fmtn2[1,3]	= "*.*lf" 10  0;		@ Format for numeric variable, no decimal	@

let fmts1[1,3]	= "-*.*s" 10 9;			@ Format for alpha, left justify		 	@
let fmts2[1,3]  = "*.*s" 10 9;			@ Format for alpha, right justify		  	@
format 10,5;							@ Default pring format						@


output file = ^outfile reset;			@ outfile is the file handle for the output file 		@
								@ Route printed output to the defined by outfile 	@
print "Results from HBMReg1.GSS";
print "Hierarchical Bayesian Multivariate Regression using MCMC.";
print "Y_{ij} = X_{ij}*beta_i + epsilon_{ij}";
print "Y_{ij} is a mvar vector";
print;
print "beta_i = Theta'z_i + delta_i";
print "or";
print "B		= Z*Theta + Delta";
print;
print "epsilon_{ij} is N(0,Sigma)";
print "delta_i is N(0, Lambda)";
print;
print "Ouput file: " getpath(outfile);		@ File assigned to file handle outfile @
datestr(date);								@ Print the current data				@
print;
print;
print "-----------------------------------------------------------------------------------";
print;
print "MCMC Analysis";
print;
print "Total number of MCMC iterations           	 = " nmcmc;
print "Number of iterations used in the analysis 	 = " smcmc;
print "Number in transition period               	 = " nblow;
print "Number of iterations between saved iterations = " skip-1;
print;
print "-----------------------------------------------------------------------------------";
print "Number of subjects	              = " nsub;
print "Number of observations per subject:";
print "     Average  = " meanc(yrows);
print "     STD      = " stdc(yrows);
print "     MIN      = " minc(yrows);
print "     MAX      = " maxc(yrows);
print;
print "Total number of Y observations     = " ntot;
print "Dimension of Y_{ij}                = " mvar;
print "Number of dependent variables X    = " rankx " (including intercept)";
print "Number of dependent variables Z    = " rankz " (including intercept)";
print;
print "First level equation:";
print "Y_{ij} = X_{ij}*beta_i + epsilon_{ij}";
print;
print "Dependent variables are " $xynames[rankxy];
print "		Summary Statistics for Y";
call sumstats(ynames,reshape(xydata[.,rankxy],ntot,mvar),fmts1,fmts2,fmtn1);
print;
print "		Summary Statistics for X";
call sumstats(xnames[2:rankx],xydata[.,2:rankx],fmts1,fmts2,fmtn1);
print;
print "Second level equation:";
print "beta_i = Theta*z_i + delta_i";
print;
print "		Summary Statistis for Z:";
call sumstats(znames,zdata,fmts1,fmts2,fmtn1);
print;
print "-----------------------------------------------------------------------------------";
print "Statistics of Fit Measures for each Dimension";
sout	= {"Variable" "Multi-R" "R-Sqr" "ErrorSTD"};
call outitle(sout,fmts1,fmts2);

bout	= ynames~multir~rsquare~stderr;
call outmat(bout,fmts1,fmtn1);
print;
print "-----------------------------------------------------------------------------------";
print;
print "Statistics for Individual-Level Regression Coefficients";
if flagtrue == 1;
	ebeta	= meanc(betat);
	sbeta	= stdc(betat);
	print "True Beta";
	sout	= {"Variable" "Mean" "STD"};
	call outitle(sout,fmts1,fmts2);
	bout = xnames~ebeta~sbeta;
	call outmat(bout,fmts1,fmtn1);
endif;

print "HB Estimates of Beta";
sout	= {"Variable" "PostMean" "PostSTD" };
call outitle(sout,fmts1,fmts2);
ebeta	= meanc(betam);
sbeta	= sqrt( meanc( (betas^2)) + stdc( betam)^2);
bout = xnames~ebeta~sbeta;
call outmat(bout,fmts1,fmtn1);
print;
print "-----------------------------------------------------------------------------------";
if flagtrue == 1;
	print "Comparison of True Beta to Individual Level Estimates";
	for i0 (1,rankx,1); i = i0;
		print "Variable " $ xnames[i];
		cb 		= corrx( betat[.,i]~betam[.,i] );
		rmse 	= betat[.,i] - betam[.,i];
		rmse	= rmse'rmse;
		rmse	= sqrt(rmse/nsub);
		print "Correlation between true and HB  = " cb[1,2];
		print "RMSE between true and HB         = " rmse;
		print;
	endfor;
endif;
print "-----------------------------------------------------------------------------------";
print "Estimation of the error covariance Sigam";
sout	= "   "~(ynames');

if flagtrue == 1;
	print "True Sigma";
	call outitle(sout,fmts1,fmts2);
	bout	= ynames~sigmat;
	call outmat(bout,fmts1,fmtn1);
	print;
endif; 
print;
print "Posterior Mean of Sigma";
call outitle(sout,fmts1,fmts2);
bout	= ynames~sigmam;
call outmat(bout,fmts1,fmtn1);
print;
print "Posterior STD  of Sigma";
call outitle(sout,fmts1,fmts2);
bout	= ynames~sigmas;
call outmat(bout,fmts1,fmtn1);
print;
print "-----------------------------------------------------------------------------------";
print;
print "HB Estimates of Theta";
sout	= "  "~(xnames');
if flagtrue == 1;
	print "True Theta";
	call outitle(sout,fmts1,fmts2);
	bout = znames~thetat;
	call outmat(bout,fmts1,fmtn1);
	print;
endif;
print "Posterior Mean of Theta";
print outitle(sout,fmts1,fmts2);
bout = znames~thetam;
call outmat(bout,fmts1,fmtn1);
print;
print "Posterior STD of Theta";
call outitle(sout,fmts1,fmts2);
bout	= znames~thetas;
call outmat(bout,fmts1,fmtn1);
print;
print "-----------------------------------------------------------------------------------";
print;
sout = "  "~(xnames');
print "HB Estimate of Lambda";
if flagtrue == 1;
	print "True Lambda";
	call outitle(sout,fmts1,fmts2);
	bout = xnames~lambdat;
	call outmat(bout,fmts1,fmtn1);
	print;
endif;
print "Posterior Mean of Lambda";
call outitle(sout,fmts1,fmts2);
bout = xnames~lambdam;
call outmat(bout,fmts1,fmtn1);
print;
print "Posterior STD of Lambda";
call outitle(sout,fmts1,fmts2);
bout = xnames~lambdas;
call outmat(bout,fmts1,fmtn1);
print;
print "===================================================================================";



output off;
closeall;
endp;




/*
*****************************************************************************************
* OUTITLE
*	Prints header for columns of numbers.
*	INPUT
*		a 	= character row vector of column names
*		fmts1	= format for first column
*		fmts2	= format for second column
*	OUTPUT
*		None
******************************************************************************************
*/
PROC (0) = outitle(a,fmt1,fmt2);
local mask, fmt, flag, ncols;
ncols	= cols(a);
mask	= zeros(1,ncols);
fmt		= fmt1|(ones(ncols-1,1).*.fmt2);
flag	= printfm(a,mask,fmt);
print;
endp;
/*
***************************************************************************************
* OUTMAT
*	Outputs a matrix:
*	(Character Vector)~(Numeric matrix);
*	The entries in the numeric matrix have the same format
*	INPUT
*		bout		= matrix to be printed
*		fmts		= format for string
*		fmtn		= format for numeric matrix
*	OUTPUT
*		None
******************************************************************************************
*/
PROC (0) = outmat(bout,fmts,fmtn);
local fmt,mask,flag,ncols, nrows;
ncols		= cols(bout);
nrows		= rows(bout);
fmt			= fmts|(ones(ncols-1,1).*.fmtn);
mask		= zeros(nrows,1)~ones(nrows,ncols-1);
flag		= printfm(bout,mask,fmt);
print;
endp;

/*
*****************************************************************************************
* SUMSTATS
*	Prints summary statistics for a data matrix
*	INPUT
*		names		= charater vector of names
*		data		= data matrix
*		fmts1		= format for string
*		fmts2		= format for string
*		fmtn		= format for numbers
*	OUTPUT
*		None
********************************************************************************************
*/
PROC (0) = sumstats(names,data,fmts1,fmts2,fmtn);
local a, bout;
a	= {"Variable" "Mean" "STD" "MIN" "MAX"};
call outitle(a,fmts1,fmts2);
bout	= names~meanc(data)~stdc(data)~minc(data)~maxc(data);
call outmat(bout,fmts1,fmtn);
endp;






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -