/******************************************************************
Scale ID: 			IPAQ-E
Scale Name: 		IPAQ Environmental Module
TILDA Variables: 	IPAQE_density; IPAQE_access; IPAQE_infrastructure; IPAQE_safety
Dataset:      		TILDA Wave 6
Author:       		
Institution:  		The Irish Longitudinal Study on Ageing (TILDA)

Description:
Generates scoring for the Penn State Worry Questionnaire.

Version:      1.0
Date:         2026-09-01
Language:     Stata

******************************************************************/

*---------------------------------------------------------------*
* Recode no-response values
*---------------------------------------------------------------*

mvdecode SCQIPAQE1-SCQIPAQE7, mv(-99=.)

* Items 2-7: 5 = Don't know / Not sure
foreach var of varlist SCQIPAQE2-SCQIPAQE7 {
    replace `var' = . if `var' == 5
}


*---------------------------------------------------------------*
* Residential density
* Item 1
*---------------------------------------------------------------*

gen IPAQE_density = .

replace IPAQE_density = 0 if SCQIPAQE1 == 1
replace IPAQE_density = 1 if inrange(SCQIPAQE1, 2, 5)

label variable IPAQE_density ///
    "IPAQ-E residential density (0=detached, 1=other housing)"


*---------------------------------------------------------------*
* Access to destinations
* Items 2, 3 and 6
* Mean score range: 1-4
*---------------------------------------------------------------*

egen IPAQE_access = rowmean( ///
    SCQIPAQE2 ///
    SCQIPAQE3 ///
    SCQIPAQE6 ///
)

* Require all three items
egen IPAQE_access_missing = rowmiss( ///
    SCQIPAQE2 ///
    SCQIPAQE3 ///
    SCQIPAQE6 ///
)

replace IPAQE_access = . if IPAQE_access_missing > 0

label variable IPAQE_access ///
    "IPAQ-E access to destinations mean score (1-4)"


*---------------------------------------------------------------*
* Neighbourhood infrastructure
* Items 4 and 5
* Mean score range: 1-4
*---------------------------------------------------------------*

egen IPAQE_infrastructure = rowmean( ///
    SCQIPAQE4 ///
    SCQIPAQE5 ///
)

* Require both items
egen IPAQE_infrastructure_missing = rowmiss( ///
    SCQIPAQE4 ///
    SCQIPAQE5 ///
)

replace IPAQE_infrastructure = . ///
    if IPAQE_infrastructure_missing > 0

label variable IPAQE_infrastructure ///
    "IPAQ-E neighbourhood infrastructure mean score (1-4)"


*---------------------------------------------------------------*
* Neighbourhood safety
* Item 7 - reverse scored
*---------------------------------------------------------------*

gen IPAQE_safety = 5 - SCQIPAQE7 ///
    if inrange(SCQIPAQE7, 1, 4)

label variable IPAQE_safety ///
    "IPAQ-E neighbourhood safety score (1-4)"


*---------------------------------------------------------------*
* Checks
*---------------------------------------------------------------*

summarize ///
    IPAQE_density ///
    IPAQE_access ///
    IPAQE_infrastructure ///
    IPAQE_safety

tab IPAQE_density, missing
tab IPAQE_safety, missing


*---------------------------------------------------------------*
* Drop temporary variables
*---------------------------------------------------------------*

drop IPAQE_access_missing IPAQE_infrastructure_missing