- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 50288 个
- 通用积分
- 83.6306
- 学术水平
- 253 点
- 热心指数
- 300 点
- 信用等级
- 208 点
- 经验
- 41518 点
- 帖子
- 3256
- 精华
- 14
- 在线时间
- 766 小时
- 注册时间
- 2006-5-4
- 最后登录
- 2022-11-6
|
- MACRO
- BayesMultReg.mac kpar y Xmat;
- sigma sig;
- prior b0 V0.
- # BayesMultReg.mac calculates the joint probability distribution for the
- # multiple linear regression model
- ###############################################################
- #
- # Instructions for using this macro
- # are contained in "Introduction to Bayesian Statistics, 3rd Edition" by
- # William M. Bolstad and James Curran (John Wiley & Sons)
- #
- ###############################################################
- #
- # Neither Minitab Inc. nor the author of this MACRO makes
- # any claim or offers any warranty whatsoever with regard to
- # the accuracy of this MACRO or its suitability for use, and
- # Minitab Inc. and the author of this MACRO each disclaims
- # any liability with respect thereto.
- #
- ###############################################################
- # MACRO NAME: BayesMultReg.mac
- # AUTHORS NAME AND ADDRESS
- # William M (Bill) Bolstad
- # Statistics Department
- # Waikato UNIVERSITY
- # Hamilton, New Zealand
- # DATE: October 2015
- mconstant nobs kpar isig sig var prb0 prb1 ma0
- mconstant df ssr xbar ybar xybar x2bar y2bar bls als ssx
- mconstant pre0 pre1 preLS k1 k2 k3 k4 kint npred kpred
- mcolumn y bL b0 B1 x0 xc.1-xc.kpar
- mcolumn fit res
- mcolumn xp yp lp up
- mmatrix Xmat X XT XTX XTXinv V0 VL V1 V0inv VLinv V1inv W W1 W2 W3 W4
- default sig=0
- # nobs is number of observations, kpred is number of predictors,
- # kpar (=kpred+1) is the number of parameters (including intercept)
- # xmat is matrix of predictors in columns xc.2-xc.kpar.
- # xc.1 is column of 1's for intercept. xc.1-xc.kpar is combined into Matrix X
- # b0 and VL are mean vector and covariance matrix of likelihood (least squares)
- # b1 and V0 are prior meanvector and covariance matrix (kpar by kpar)
- # bL and V1 are posterior mean vector and covariance matrix (kpar by kpar)
- # sig is standard deviation. When it is not input, estimate
- # calculated from residuals is used which gives approximation.
- # ia indicator for intercept prior, 0=flat prior, 1=normal prior
- # ib indicator for coefficients prior, 0=joint flat prior, 1=joint normal prior
- let nobs=n(y)
- ## prepare matrix X including column for intercept
- copy Xmat xc.2-xc.kpar
- set xc.1
- nobs(1)
- end
- copy xc.1-xc.kpar X
- ## Find least squares vector (MLE) and covariance matrix
- trans X XT
- mult XT X XTX
- inve XTX XTXinv
- mult XTXinv XT W
- mult W y bL
- print bL
- mult X bL fit
- let res=y-fit
- if (sig=0)
- let df=nobs-kpar
- let ssr=sum(res**2)
- let sig=sqrt(ssr/df)
- print df ssr sig
- let var=sum(res**2)/(df)
- let sig=sqrt(var)
- print "Standard deviation of residuals" sig
- elseif (sig > 0)
- print "known standard deviation" sig
- let var=sig**2
- let isig=1
- endif
- mult var XTXinv VL
- print bL VL
- inve V0 V0inv
- inve VL VLinv
- add V0inv VLinv V1inv
- inve V1inv V1
- print V0inv VLinv V1inv V1
- mult V1 V0inv W1
- mult V1 VLinv W2
- print w1 w2
- mult W1 b0 W3
- mult W2 bL W4
- add W3 W4 b1
- print w3 w4
- print b0 V0 bL VL b1 V1
- endmacro
复制代码
|
|