process-NEED-EULF-2014.do 8.45 KB
Newer Older
Ben Anderson's avatar
Ben Anderson committed
1
2
3
4
5
6
/* 
Script to turn original wide 2014 EULF version of DECC's NEED data into:
 1. a stata wide form xwave file containing the fixed value variables
 2. a stata wide form file containing just the yearly consumption variables (linked to 1. via HH_ID)
 3. a stata long form file containing just the yearly consumption variables (linked to 1. via HH_ID)
 4. Create codebooks from the above
Ben Anderson's avatar
Ben Anderson committed
7

Ben Anderson's avatar
Ben Anderson committed
8
Original data available from: UK DATA ARCHIVE: Study Number 7518 - National Energy Efficiency Data-Framework, 2014
Ben Anderson's avatar
Ben Anderson committed
9

Ben Anderson's avatar
Ben Anderson committed
10
http://discover.ukdataservice.ac.uk/catalogue/?sn=7518
Ben Anderson's avatar
Ben Anderson committed
11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Copyright (C) 2014  University of Southampton

Author: Ben Anderson (b.anderson@soton.ac.uk, @dataknut, https://github.com/dataknut) 
	[Energy & Climate Change, Faculty of Engineering & Environment, University of Southampton]

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License 
(http://choosealicense.com/licenses/gpl-2.0/), or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

#YMMV - http://en.wiktionary.org/wiki/YMMV

Ben Anderson's avatar
Ben Anderson committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
******************
Notes:
This dataset is a sample of just over 4 million households which have had an Energy Performance Certificate from the full NEED 'all dwellings' dataset
Is this all those who have had an EPC or a random sample of all those who've had an EPC?
Sample bias is unkown - which kinds of dwellings have an EPC?
Ideally DECC should set missing to -99 to aid re-coding and avoid unpleasant surprises in naive analysis!
The Gcons*valid variable codes:
    G = Gas consumption invalid, greater than 50,000
    L = Gas consumption invalid, less than 100
    M = Gas consumption data is missing in source data
    0 = Property does not have a gas connection
    V = Valid gas consumption (between 100 and 50,000 inclusive)
    NB - there are valid gas readings of '0' which presumably were > 100 but < 249 (first gas 'heap' = 'nearest 500')
The Econs*valid variable codes:
    G Electricity consumption invalid, greater than 25,000 (DECC lookup table says 50,000)
    L Electricity consumption invalid, less than 100
    M Electricity consumption data is missing in source dataset
    V Valid electricity consumption (between 100 and 25,000 inclusive)

48
*/
Ben Anderson's avatar
Ben Anderson committed
49
50
51
52
53
54
55
56
57
58
59
60
61

clear all

capture noisily log close _all

* written for Mac OSX - remember to change filesystem delimiter for other platforms
local home "/Users/ben/Documents"
local proot "`home'/Work/Data/Social Science Datatsets/DECC"
* for clam
* local proot "`home'/Work/NEED"
local dpath "`proot'/NEED/End User Licence File 2014/"
* NB this is the 2014 EULF we're using
local ifile "need_eul_may2014"
62
* original data file
63
local dfile_orig "`dpath'UKDA-7518-stata11/stata11/`ifile'"
Ben Anderson's avatar
Ben Anderson committed
64

65

66
67
* If you really wanted to you could set up a loop to iterate over a list of sample values to create a set of random sub-samples
* For now we'll just create a random sub-sample of sample% to make testing models etc easier
68
* 10 = 10% sample, 50 = 50% sample, 100 = 100% sample etc
69
70
local sample "10 50 100"

Ben Anderson's avatar
Ben Anderson committed
71
72
73
74
75
76
77
local sampleby "EE_BAND PROP_TYPE"

local version "v1.1"
* includes production of % samples which maintain the original dimensions used to 
* produce the EULF samples: EE_BAND PROP_TYPE

*local version "v1"
Ben Anderson's avatar
Ben Anderson committed
78
79
80
81
82

set more off

log using "`dpath'/processed/process-NEED-EULF-2014-`version'-$S_DATE.smcl", replace name(main)

83
84
* use these locals to control what happens (set to 0 to skip the code)
* create codebook & some descriptives
85
local create_codebook = 1
Ben Anderson's avatar
Ben Anderson committed
86
87
88
* create wide form fixed file with (supposedly) unchanging data & a seperate 'wide' consumption data file for cross-sectional analysis
local create_xwavefile = 1
* create long form file with wave (yearly) data - be careful, this take a long time due to large memory use!
Ben Anderson's avatar
Ben Anderson committed
89
90
91
local create_longfile = 1

* load the original file
92
use "`dfile_orig'.dta", clear
Ben Anderson's avatar
Ben Anderson committed
93
94

if `create_codebook' {
Ben Anderson's avatar
Ben Anderson committed
95
	* create original EULF codebook
96
	* not much point running this for each % sample although the counts etc reported in the codebook won't match
Ben Anderson's avatar
Ben Anderson committed
97
	log off main
Ben Anderson's avatar
Ben Anderson committed
98
99
100
101
		log using "`dfile_orig'-codebook-$S_DATE.smcl", replace name(cb)
		desc
		codebook
		log close cb
Ben Anderson's avatar
Ben Anderson committed
102
103
104
	log on main
}

Ben Anderson's avatar
Ben Anderson committed
105

106
107
108
109
110
111
foreach s of local sample {
	local samplet "`s'pc"
	***** random sample ****
	* select a random sample but ensure proportions of sampleby are kept
	di "* Keeping `s'% sample by `sampleby'"
	sample `s', by(`sampleby')
Ben Anderson's avatar
Ben Anderson committed
112
	
113
	tab `sampleby', mi
Ben Anderson's avatar
Ben Anderson committed
114
	
115
116
117
118
119
120
121
122
123
124
	
	if `create_xwavefile' {
		* create the file with data that (notionally) doesn't change
	
		* create a wide consumption file
		preserve
			keep HH_ID Gcons* Econs*
			compress
			save "`dpath'/processed/`ifile'_consumptionfile_wide_`samplet'.dta", replace
		restore
125
126
		
		
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		preserve
			drop Gcons* Econs*
			* fix some mis-codings (or lack of coding of missing)
			local vars "E7Flag2012 CWI LI BOILER"
			foreach v of local vars {
				destring `v', force replace
				replace `v' = 0 if `v' !=1
				label def `v' 0 "No or N/A" 1 "Yes"
				label val `v' `v'
			}
			
			* turn '99' into missing - ideally missing should be -99 to aid re-coding and avoid unpleasant surprises in naive analysis!
			* recode category vars into strings to avoid confusion & enable 'unknown' to be retained in models etc (might matter)
			tostring LOFT_DEPTH FP_ENG LI_YEAR BOILER_YEAR CWI_YEAR, force replace
			replace FP_ENG = "Unknown" if FP_ENG  == "99"
			replace LOFT_DEPTH = "Unknown" if LOFT_DEPTH  == "99"
			* comes in as missing?
			replace LI_YEAR = "Unknown" if LI_YEAR == "."
			replace BOILER_YEAR = "Unknown" if BOILER_YEAR == "."
			replace CWI_YEAR = "Unknown" if CWI_YEAR == "."
					
			* what do G, L, M mean in the gas 'valid' variables - presumably 0 = off gas & V = valid?
			
			tabstat IMD_WALES, by(REGION) s(mean min max n)
			* there seem to be some welsh LSOAs allocated to English GORs?
			
			tabstat IMD_ENG, by(REGION) s(mean min max n)
			* there seem to be some English LSOAs allocated to Wales?
155
		
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
			tab FP_ENG REGION
			
			* REGION is ONS admin codes
			* create a new variable with meaningful labels
			gen ba_region = 1 if REGION == "E12000001"
			replace ba_region = 2 if REGION == "E12000002"
			replace ba_region = 3 if REGION == "E12000003"
			replace ba_region = 4 if REGION == "E12000004"
			replace ba_region = 5 if REGION == "E12000005"
			replace ba_region = 6 if REGION == "E12000006"
			replace ba_region = 7 if REGION == "E12000007"
			replace ba_region = 8 if REGION == "E12000008"
			replace ba_region = 9 if REGION == "E12000009"
			replace ba_region = 10 if REGION == "W99999999"
			
			lab var ba_region "former Govt Office region (labelled)"
			* http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/administrative/england/government-office-regions/index.html
			lab def ba_region 1 "North East" 2 "North West" 3 "Yorkshire & The Humber" 4 "East Midlands" ///
				5 "West Midlands" 6 "East of England" 7 "London" 8 "South East" 9 "South West" 10 "Wales"
			lab val ba_region ba_region
			
			compress
			log off main
Ben Anderson's avatar
Ben Anderson committed
179
180
181
182
183
184
185
				log using "`dpath'/processed/codebook-NEED-EULF-2014-`ifile'_xwavefile-`version'-`samplet'-$S_DATE.smcl", replace name(cb_xwave)
				desc
				di "** urban/rural would be helpful"
				codebook
				compress
				save "`dpath'/processed/`ifile'_xwavefile_`samplet'.dta", replace
				log close cb_xwave
186
187
188
			log on main
		restore
	}
Ben Anderson's avatar
Ben Anderson committed
189
	
190
191
192
	if `create_longfile' {
		* create the long file with as few vars as possible (quicker)
		* still takes a while...
Ben Anderson's avatar
Ben Anderson committed
193
194
		preserve
			keep HH_ID Gcons* Econs*
195
		
Ben Anderson's avatar
Ben Anderson committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
			* panel vars:
			* Gcons2005 Gcons2005Valid Econs2005 Econs2005Valid -> 2012
			local vars "Gcons Econs"
			foreach v of local vars {
				di "* Renaming -> `v'"
				foreach y of numlist 2005/2012 {
					di "* -> `v' (`y')"
					* put year on the end so reshape works
					gen `v'Valid`y' = `v'`y'Valid
					* remove old variable to save time in reshape & space
					drop `v'`y'Valid
				}
			}
			* this takes a LONG time for the full dataset
			reshape long Gcons GconsValid Econs EconsValid, i(HH_ID)
			rename _j year
			* set as panel
			xtset HH_ID year
			compress
			save "`dpath'/processed/`ifile'_consumptionfile_long_`samplet'.dta", replace
			
			log off main
Ben Anderson's avatar
Ben Anderson committed
218
219
220
221
				log using "`dpath'/processed/codebook-NEED-EULF-2014-`ifile'-long-`version'-`sample'pc-$S_DATE.smcl", replace name(cbl)
				desc
				codebook
				log close cbl
Ben Anderson's avatar
Ben Anderson committed
222
223
224
			log on main
		restore
		* this leaves us with the original file in memory
Ben Anderson's avatar
Ben Anderson committed
225
226
227
	}
}

228
229
* done!

Ben Anderson's avatar
Ben Anderson committed
230
log close _all