*catdap02.sps------------------------------------------------. * catdapj拡張マクロ for spss. by k.hori(hori@ec.kagawa-u.ac.) * 1996.8.22. rev.2005.7.26. * 2005.8.31. クロス表追加. * 2005.9.08. 2元クロスだけの処理を可能にした. * 2005.10.20. rev. *-------------------------------------------------------------. *example. /*catdap2 ivar=OPTHALMO ECG AGE /cvar=DIAGNOS/TEMP=YES */ * ~~^^^^^^^^^^^^^^^ 反応変数 ^^^ . * 説明変数 現行データを保存し呼び出す. * ivar=説明変数リスト/cvar=反応変数/temp=yes[default=no}/ * /loading='フルパスファイル名'(処理後呼び出すSPSSデータファイル) * /third=3 (説明変数を3つ使う、既定値 0 は使わない). * /zero=0.3678794411714423 セルの頻度が0の時に代入する値。既定値 0.5. define catdap2 (ivar=!charend('/') /cvar=!charend('/') /loading=!default(NO)!charend('/') /temp=!default(NO)!charend('/') /third=!default(2)!charend('/') /zero=!default(0.5)!charend('/') ). *----------------------------------------------. preserve. set printback=off. set length=none. !let !three=!third . *!let !zero=0.5. *!let !zero=0.3678794411714423. /* exp(-1) */ set mxloop=4000. !let !tempf="c_temp_.sav". !if (!tail(!ivar) !eq !null) !then . !let !nivar=1. !else. !if (!tail(!tail(!ivar)) !eq !null) !then. !let !nivar=2. !else. !let !nivar=3. !ifend. !ifend. !if (!length(!temp)>2) !then. save outfile=!tempf . !ifend. !if (!third >3) !then. !let !three=3. !ifend. *****************************************************************. matrix. compute colname={"****** ","catdap2","******"," ", "v.1.0 ","2005/9/8","k.hori"}. print colname/format=a10/title=' '. compute nprint=8. *design の出力を1,2,3...とするため num. compute num={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, 23,24,25,26,27,28,29,30}. compute num=t(num). compute numlab={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"}. get x /file=*/variables=!ivar !cvar /missing=omit/sysmis=omit/names=nx. *変数数 #nvar. compute #nvar=ncol(nx)-1. compute resname=nx(ncol(nx)). compute nx=nx(1,1:#nvar). compute nsample=nrow(x). *カテゴリの1,0展開 結果を d に 反応変数. compute x1=x(:,(#nvar+1)). compute gmax=cmax(x1). compute x1={num(1:gmax,1);x1}. compute #row1=nsample+gmax. compute d=design(x1). compute d=d((gmax+1):#row1,:). * 空の列をチェック. compute col0=csum(d). * print col0. do if (mmin(col0)=0). compute ncol0=ncol(col0). loop i=ncol0 to 1 by -1. do if (col0(i)=0). do if (i=ncol0). compute d0=d(:,1:(ncol(d)-1)). end if. do if (i=1). compute d0=d(:,2:ncol(d)). end if. do if (i>1 and i 2)) . compute aend=0. else. compute aend=#nvar-2. end if. *-------------------------. loop a=-1 to aend. do if (a=-1). compute bend=a+1. else. compute bend = #nvar-1. end if. do if (three=1). compute bend=0. end if. *print bend. loop b=a+1 to bend. loop c=(b+1) to #nvar. * 説明変数の合成. compute ii=ii+1. compute aa=make(nsample,1,0). do if (a>0) . compute aa=aa+(x(:,a)-1)&*(cmax(x(:,b))&*cmax(x(:,c))). end if. do if (b>0) . compute aa=aa+(x(:,b)-1)&*cmax(x(:,c)). end if. compute aa=aa+x(:,c). compute #nnum=mmax(num). compute #mxaa=mmax(aa). do if (#mxaa>#nnum). loop inum=(#nnum+1) to #mxaa. compute num={num;inum}. end loop. end if. *カテゴリの1,0展開 結果を z に. compute cmaxs=cmax(aa). compute x1=aa. compute x1={num(1:cmaxs,1);x1}. compute z=design(x1). compute #row1=nsample+cmaxs. compute z=z((cmaxs+1):#row1,:). *カテゴリ総数 ctotal. compute ctotal=ncol(z). release x1. *目的変数と合成変数のクロス表作成. compute z=t(d)*z. compute z2={z;csum(z)}. compute z2={z2,rsum(z2)}. *print z2. *空の列をチェック. compute col0=csum(z). * print col0. do if (mmin(col0)=0). compute ncol0=ncol(col0). * print ncol0 /title "loop前". loop i=ncol0 to 1 by -1. * print z/title "loop内". do if (col0(i)=0). do if (i=ncol0). compute z0=z(:,1:(ncol(z)-1)). end if. do if (i=1). compute z0=z(:,2:ncol(z)). end if. do if (i>1 and i=0) . *or (vnum(i,1)=-1)). do if (i>10). compute i=10. end if. *print best10. do if (best10(i)<=1 ). compute i=i-1. end if. *print i. compute iend = i. *----------------------------------------------------------------. do if (iend > 0). *print iend. loop i = 1 to iend. print i/title="順位".. *クロス表作成. * 説明変数の合成. compute aa=make(nsample,1,0). compute c=vnum(best10(i),1). compute b=vnum(best10(i),2). compute a=vnum(best10(i),3). compute acat=-1. do if (a >0) . compute aa=aa+(x(:,a)-1)&*(cmax(x(:,b))&*cmax(x(:,c))). compute acat=cmax(x(:,a)). end if. compute bcat=-1. do if (b>0) . compute aa=aa+(x(:,b)-1)&*cmax(x(:,c)). compute bcat=cmax(x(:,b)). end if. compute aa=aa+x(:,c). compute ccat=cmax(x(:,c)). * end if. *print {acat,bcat,ccat}. compute #nnum=mmax(num). compute #mxaa=mmax(aa). do if (#mxaa>#nnum). loop inum=(#nnum+1) to #mxaa. compute num={num;inum}. end loop. end if. *カテゴリの1,0展開 結果を z に. compute cmaxs=cmax(aa). compute x1=aa. compute x1={num(1:cmaxs,1);x1}. compute z=design(x1). compute #row1=nsample+cmaxs. compute z=z((cmaxs+1):#row1,:). *カテゴリ総数 ctotal. compute ctotal=ncol(z). release x1. *目的変数と合成変数のクロス表作成. compute z=t(t(d)*z). compute z2={z;csum(z)}. compute zsum=rsum(z2). compute z3={z2,zsum}. *print z3. *説明変数カテゴリ表作成. do if (acat>0). compute iname={nx(a),nx(b),nx(c)}. * print aname/format a8. compute ecat={1}. loop j=2 to ccat. compute ecat={ecat;j}. end loop. compute ebcat={make(ccat,1,1),ecat}. loop j=2 to bcat. compute ebcat2={make(ccat,1,j),ecat}. compute ebcat={ebcat;ebcat2}. end loop. compute tcat=nrow(ebcat). compute eccat={make(tcat,1,1),ebcat}. loop j=2 to acat. compute eccat2={make(tcat,1,j),ebcat}. compute eccat={eccat;eccat2}. end loop. compute eccat={eccat;make(1,3,0)}. compute ecat=eccat. * print ecat. else if (bcat>0). * print bcat. compute iname={nx(b),nx(c)}. compute ecat={1}. loop j=2 to ccat. compute ecat={ecat;j}. end loop. compute ebcat={make(ccat,1,1),ecat}. loop j=2 to bcat. compute ebcat2={make(ccat,1,j),ecat}. compute ebcat={ebcat;ebcat2}. end loop. compute ebcat={ebcat;make(1,2,0)}. compute ecat=ebcat. else. compute iname=nx(c). compute ecat={1}. loop j=2 to ccat. compute ecat={ecat;j}. end loop. compute ecat={ecat;0}. end if. * print ecat. *ラベル作成. compute clab={iname, numlab(1:ncol(z2)),"合計"}. compute rlab={make((nrow(z2)-1),1," ");"合計"}. *表出力. * compute clab2={ "順位","AIC","目的変数"," = ", resname}. *print {i,aic(best10(i)),0,0,0}/title=" "/cnames=clab2/format f8.3. print aic(best10(i))/title="AIC"/format f8.3. print {" = ",resname}/format a8/rlabels= "目的変数"/title=" ". print {ecat,z3}/titel="頻度表"/cnames=clab/rnames=rlab. compute z3=z3*100. compute zsum2=zsum. loop k=1 to ncat. compute zsum2={zsum2,zsum}. end loop. compute zsum0=make(nrow(zsum2),ncol(zsum2),0). * print zsum0. *print zsum2. compute zsum0=(zsum2=zsum0). compute zsum2=zsum2+zsum0. compute z3=z3&/zsum2. print {ecat,z3}/titel="パーセンテージ"/cnames=clab/rnames=rlab/format=f5.1. end loop. end if. end matrix. format df(f5.0). sort cases by aic. !let !t=!concat('response variable = ',!cvar). !let !t=!quote(!t). temporary. select if (nv=1). report format=list/var=v1 'explanatatory' ' variables' df 'number of' 'categories' aic 'A I C' /title=left 'number of explanatory variables = 1' !t. !if (!nivar>1.5) !then. !if (!third > 1.5) !then. temporary. select if (nv=2). report format=list/var=v1 'explanatatory' v2 'variables' df 'number of' 'categories' aic 'A I C'/ title=left 'number of explanatory variables = 2' !t. !if (!nivar>2.5) !then. !if (!third > 2.5) !then. temporary. select if (nv=3). n of cases 100. report format=list/var=v1 'explanatatory' v2 'variables' v3 ' ' df 'number of' 'categories' aic 'A I C' /title=left 'number of explanatory variables = 3' !t. !ifend. !ifend. !ifend. !ifend. temporary. n of cases 100. report format=list/var=v1 'explanatatory' v2 'variables' v3 ' ' df 'number of' 'categories' aic 'A I C' /title=left 'Summary of Subsets of Explanatory Varialbes' !t. !if (!length(!temp) > 2) !then. get file=!tempf. !ifend. restore. !enddefine. *=================================================. . catdap2 ivar=t30008 t30010/cvar=t30011/TEMP=YES/zero=0.5.