Block code: logreg_stats
The block named logreg_stats
is contained in the library
regression_statistics
, which can be found in
pub.lib.modeling.
<library name="regression_statistics">
<block name="logreg_stats" group="" model="" target="" arg_list="" get="pv" ns="">
<note>*********************************************************</note>
<note>*** Extract Logistic Regression Fit Statistics: ****</note>
<note>*** - Deviance (-2*Log Likelihood) ****</note>
<note>*** - AIC ****</note>
<note>*** AND, depending on value of get parameter: ****</note>
<note>*** - Parameter p-values (get=pv) ****</note>
<note>*** - Parameter standard errors (get=se) ****</note>
<note>*** - Parameter z-values (get=zv) ****</note>
<note>*** - All of the above (get=all) ****</note>
<note>*** ****</note>
<note>*** Allowed arguments: ****</note>
<note>*** - group: pass same field(s) as used in G ****</note>
<note>*** argument of g_logreg (may be blank) ****</note>
<note>*** - model: Required, logreg model column ****</note>
<note>*** - target: Required, logreg dependent variable ****</note>
<note>*** - arg_list: Required, comma- or space- ****</note>
<note>*** separated list of independent variables ****</note>
<note>*** - get: Required, see options above ****</note>
<note>*** - ns: Optional, namespace suffix which will be ****</note>
<note>*** appended to all column names created by ****</note>
<note>*** this block to prevent name collisions ****</note>
<note>*********************************************************</note>
<let arg_list_csl="{if(contains(@arg_list;',');@arg_list;strsubst(@arg_list;' ';0;','))}">
<if test="{(strpick(@arg_list_csl;',';1)='1')|(strpick(@arg_list_csl;',';-1)='1')|(strfind(@arg_list_csl;',1,';1)<>na)}">
<signal msg="logreg_stats Error: Invalid arg_list - arg_list should contain only predictor variables and no constant term (1)."/>
</if>
<willbe name="p{@ns}" value="score(1 {@arg_list_csl};{@model};)"/>
<willbe name="z{@ns}" value="loge(p{@ns}/(1-p{@ns}))"/>
<willbe name="likelihood_tmp{@ns}" value="-({@target}*loge(p{@ns}/(1-p{@ns}))+(loge(1-p{@ns})))"/>
<willbe name="nlog_lik{@ns}" value="g_sum(seg_ {@group};;likelihood_tmp{@ns})"/>
<willbe name="p_null{@ns}" value="g_avg(seg_ {@group};;{@target})"/>
<willbe name="likelihood_null_tmp{@ns}" value="-({@target}*loge(p_null{@ns}/(1-p_null{@ns}))+(loge(1-p_null{@ns})))"/>
<willbe name="nlog_lik_null{@ns}" value="g_sum(seg_ {@group};;likelihood_null_tmp{@ns})"/>
<willbe name="deviance{@ns}" value="-2*nlog_lik{@ns}"/>
<willbe name="aic{@ns}" value="2*{strcount(@arg_list_csl;',')+2}+2*nlog_lik{@ns}"/>
<willbe name="x_0{@ns}" value="sqrt(p{@ns}*(1-p{@ns}))"/>
<foreach i="{@arg_list_csl}">
<willbe name="{@i}__x{@ns}" value="{@i}*sqrt(p{@ns}*(1-p{@ns}))"/>
</foreach>
<letseq tmp="{splice('__x' ',';@ns)}" lsq_arg_list="{strsubst(@arg_list_csl;',';0;@tmp)}__x{@ns}">
<willbe name="lsq_model{@ns}" value="g_lsq(seg_ {@group};;z{@ns};x_0{@ns},{@lsq_arg_list})"/>
</letseq>
<let args="const,{@arg_list_csl}">
<switch on="{@get}">
<case when="se">
<for i="1" to="{strcount(@arg_list_csl;',')+2}">
<willbe name="se_{strpick(@args;',';@i)}{@ns}" value="sqrt(param(lsq_model{@ns};'g';{@i}))"/>
</for>
</case>
<case when="zv">
<for i="1" to="{strcount(@arg_list_csl;',')+2}">
<willbe name="z_value_{strpick(@args;',';@i)}{@ns}" value="param({@model};'b';{@i})/sqrt(param(lsq_model{@ns};'g';{@i}))"/>
</for>
</case>
<case when="pv">
<for i="1" to="{strcount(@arg_list_csl;',')+2}">
<willbe name="p_value_{strpick(@args;',';@i)}{@ns}" value="2*(1-normal_cdf(abs(param({@model};'b';{@i})/sqrt(param(lsq_model{@ns};'g';{@i})));0;1))"/>
</for>
</case>
<case when="all">
<for i="1" to="{strcount(@arg_list_csl;',')+2}">
<willbe name="se_{strpick(@args;',';@i)}{@ns}" value="sqrt(param(lsq_model{@ns};'g';{@i}))"/>
</for>
<for i="1" to="{strcount(@arg_list_csl;',')+2}">
<willbe name="z_value_{strpick(@args;',';@i)}{@ns}" value="param({@model};'b';{@i})/sqrt(param(lsq_model{@ns};'g';{@i}))"/>
</for>
<for i="1" to="{strcount(@arg_list_csl;',')+2}">
<willbe name="p_value_{strpick(@args;',';@i)}{@ns}" value="2*(1-normal_cdf(abs(param({@model};'b';{@i})/sqrt(param(lsq_model{@ns};'g';{@i})));0;1))"/>
</for>
</case>
</switch>
</let>
</let>
</block>
</library>