Stata Code: textscore

version 7.0

#delimit ;

global WSversion "0.36";

program define textscore, rclass;
    local dimension "`1'";
    macro shift; 
    global vtextnames "`*'";

    tempvar totwords;
    quietly gen `totwords' = .;
/*    tempvar tempv;
    quietly gen `tempv' = 0; */
    local Rvdtot = 0;

* set trace on;
    while "`1'" ~= "" {;
	tempvar f`1' d`1' v`1';
	quietly replace `totwords' = sum(`1') if `dimension'~=.;
	quietly summarize `totwords';
        quietly gen `f`1'' = `1' / r(max) if `dimension'~=.;
	quietly gen `d`1'' = sum(`f`1'' * `dimension');
	local Rvd`1' = `d`1''[_N];
	local Rvdtot = `Rvdtot' + `Rvd`1'';
	quietly gen `v`1'' = sum(`f`1'' * ( `dimension'  - `d`1''[_N] )^2);
	macro shift;
    };
 
    /* get the mean, N, stdev of virgin scores needed for transform */
    local RvdN : word count $vtextnames;
    local Rvdmean = `Rvdtot' / `RvdN';
    local Rvdsqdev = 0;
    tokenize $vtextnames;
    while "`1'" ~= "" {;
	local Rvdsqdev = `Rvdsqdev' + (`Rvd`1'' - `Rvdmean')^2;
	macro shift;
    };
    local RvdSD = sqrt(`Rvdsqdev' / (`RvdN' - 1));

* set trace on;
    tempvar tempv;
    tokenize $vtextnames;
    while "`1'" ~= "" {;
        local RvdStd`1'  = (`Rvd`1'' - `Rvdmean') / `RvdSD';
	local RvdUstd`1' = (`RvdStd`1'' * $RdSD) + `Rvdmean';
	/* note: here the addition assumes virgin anchor mean */

	/* now do the transformed CIs and SEs */
	quietly gen `tempv' = sum(`1') if `dimension'~=.;
	quietly summ `tempv';
	local Vscored = r(max);
	drop `tempv';
	local RawSE`1' = sqrt(`v`1''[_N]) / sqrt(`Vscored');
	local RawSEx2 = 2*(sqrt(`v`1''[_N]) / sqrt(`Vscored'));
*	display `RawSEx2';
	local t = ((`Rvd`1'' - `RawSEx2') - `Rvdmean') / `RvdSD';
*	display `t';
	local CIlo`1' = (`t' * $RdSD) + `Rvdmean';
	local t = ((`Rvd`1'' + `RawSEx2') - `Rvdmean') / `RvdSD';
	local CIhi`1' = (`t' * $RdSD) + `Rvdmean';
	local UstdSE`1' = ((`Rvdmean' - `CIlo`1'') + (`CIhi`1'' - `Rvdmean')) / 2 / 2;

	macro shift;
    };


    display in text "";
    display in text "Wordscore v$WSversion  (c) 2003 Kenneth Benoit";
    display in text "Dimension: " upper("`dimension'");

    display in text;
    display in text "            {c |}                    Unique    Trans-    Trans-                          Total      %";
    display in text "     Virgin {c |}     Raw      Raw   Scored    formed    formed       Transformed        Words    Tot";
    display in text "       Text {c |}   Score       SE    Words     Score        SE   [95% Conf. Interval]  Scored   Sc'd";
    display in text "{hline 12}{c +}{hline 85}";

    tokenize $vtextnames;
    tempvar uniqtemp vwords;
    while "`1'" ~= "" {;
	format %10.4f `d`1'';
        quietly gen `uniqtemp' = `1' if `dimension'~=. & `1'>0;
	quietly replace `uniqtemp' = sum(`uniqtemp');
	quietly gen `vwords' = sum(`1'); 
	quietly summ `1' if `dimension'~=. & `1'>0;
        display in text %11s abbrev("`1'",11) " {c |}" as result
    	                %8.4f `Rvd`1'' /* `d`1''[_N] */
    	                %9.4f  `RawSE`1''
                        %9.0gc  r(N)
			%10.4f `RvdUstd`1'' 
			%10.4f `UstdSE`1''
			%11.4f `CIlo`1''
			%11.4f `CIhi`1''
			%9.0gc `uniqtemp'[_N]
			%7.1f `uniqtemp'[_N]/`vwords'[_N]*100
			;
	quietly drop `uniqtemp' `vwords';
	macro shift;
    };

set trace off;
end;