wiki:FourthQuery
Last modified 12 years ago Last modified on 10/17/06 16:44:09

return to XbrainEvaluation

(: variable to specify threshold for dividing between younger and older patients :)
let $age_threshold := 50

(: gather all patients with naming errors :)
let $patients_with_naming_errors :=
	dxq:csm("
		<results>
		{
			for $p in $pv/patient[surgery/csmstudy/trial[stimulated='Y']/trialcode/term[type='CSM error code']]
			return 
				<patient>
					{$p/pnum}
					{$p/sex}
					<age>{data($p/age_at_registration)}</age>
					{
						for $trial in $p/surgery/csmstudy/trial[stimulated='Y']
						where exists($trial/trialcode/term[type='CSM error code'])
						return 
							<trial>
								{$trial/_oid}
								<trialcodes>
								{
									for $code in $trial/trialcode/term[type='CSM error code']/abbrev
									return
										<trialcode>{$code/text()}</trialcode>
								}
								</trialcodes>
							</trial>
					}
				</patient>
		} 
		</results>
	")
	
(: divide patients with naming errors into younger and older groups :)
let $younger_patients := $patients_with_naming_errors//patient[number(age)<$age_threshold]
let $older_patients := $patients_with_naming_errors//patient[number(age)>=$age_threshold]

(: generate lists of NUMERIC error codes exhibited by patients for each group :)
let $y_error_codes := 
	for $error_code in $younger_patients//trialcode/text()
	where string(number($error_code)) != 'NaN'
	return 
		$error_code
		
let $o_error_codes := 
	for $error_code in $older_patients//trialcode/text()
	where string(number($error_code)) != 'NaN'
	return 
		$error_code

(: calculate code counts for younger patients :)
let $y_code_counts :=
	for $code in distinct-values($y_error_codes)
	let $code_count := count(index-of($y_error_codes,$code))
	order by -$code_count
	return
	<code_count>
		<code>{$code}</code>
		<count>{$code_count}</count>
	</code_count>
	
(: calculate code counts for older patients :)
let $o_code_counts :=
	for $code in distinct-values($o_error_codes)
	let $code_count := count(index-of($o_error_codes,$code))
	order by -$code_count
	return
	<code_count>
		<code>{$code}</code>
		<count>{$code_count}</count>
	</code_count>
	
return
<results>
	<younger_patients>
	{
		for $code_count in $y_code_counts
		let $code_percentage := 
			(: format to 2 significant figures after the decimal :)
			let $full_code_percentage := string($code_count/count div count($y_error_codes) * 100)
			return
				concat(substring-before($full_code_percentage,'.'),'.',
					substring(substring-after($full_code_percentage,'.'),1,2))
		return
			<error freq="{concat($code_percentage,'%')}">{$code_count/code/text()}</error>
	}
	</younger_patients>
	<older_patients>
	{
		for $code_count in $o_code_counts
		let $code_percentage := 
			(: format to 2 significant figures after the decimal :)
			let $full_code_percentage := string($code_count/count div count($o_error_codes) * 100)
			return
				concat(substring-before($full_code_percentage,'.'),'.',
					substring(substring-after($full_code_percentage,'.'),1,2))
		return
			<error freq="{concat($code_percentage,'%')}">{$code_count/code/text()}</error>
	}
	</older_patients>
</results>