## [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
rm(list=ls())
library(lme4)
## load in energy data
setwd("//soton.ac.uk/ude/PersonalFiles/Users/xl25g12/mydocuments/census2022/CER_analysis/data")## the file is in working order
## egdata = read.table("OctHH_midwk_long", header=T, stringsAsFactors=FALSE) ## read in R exported txt file, no need sep="t"; long form of mid week data for all Oct 2009 and 2010 data
egdata=read.table("CER_OctHH_wkend_long",header=T,stringsAsFactors=FALSE)## read in R exported txt file, no need sep="t"; long form of mid week data for all Oct 2009 and 2010 data
eg09=egdata[egdata$DateOct<365,]
eg10=egdata[egdata$DateOct>365,]
## load in HH survey data, note survey content for pre-trial and post-trial are different
data1=read.table("HH09_pretrial.txt",header=T,stringsAsFactors=FALSE,sep="\t")## read in txt file, need sep="t", HHpresurvey data
HH09FA=read.table("HH_floor_pretrial_survey.txt",header=T,stringsAsFactors=FALSE,sep="\t")## read in txt file, need sep="t"; pretrial floor area data
## HH10 = read.table("HH10_posttrial.txt",header=T,stringsAsFactors=FALSE,sep="\t") ## read in txt file, need sep="t", HHpresurvey data
HH09=merge(data1,HH09FA)
## merge energy and HH data together
## data for 09 and 10 separately
mydata09=merge(eg09,HH09)
##mydata10=merge(eg10,HH10)
########## mixed effects models are run on these separate files ###################
## check on mixed model with income variables using 09 data
## make a comparison with 0910 data results using 0910
## for 1 RE only: Extract random effect varaince and percentage of ICC (intercorrelation between RE in intercept and residuals)
## command: as.data.frame(VarCorr(test)) gives variance-covariance of the model: as.data.frame(VarCorr(test))$vcov; std.error of the model: as.data.frame(VarCorr(test))$sdcor
RE1=as.data.frame(VarCorr(T2))$vcov[1]## variance of RE in the intercept "ID"
RE2=as.data.frame(VarCorr(T2))$vcov[2]## varaince of residual ##
testlmer=as.vector(T2@pp$X%*%fixef(T2))## fitted values ## length of fitted values is 4099, no adjustment is made
VarF=var(testlmer)
## VarV = variance (intercept) + varaince(Slope) + 2 cov.correlation * stdev(intercept)stdev(slope) ## variation generated from RE
##### prepare for dummy_em, with 1-3 value of employment variable as 0, 4-6 as 1 ######
mydata0910["dum_em13_46"]=NA
mydata0910=within(mydata0910,dum_em13_46[employment<4]<-0)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
mydata0910=within(mydata0910,dum_em13_46[employment>3]<-1)## 1 for unemployed people: , 4: unemployed actively seeking work; 5 unemployed not seeking work, 6: retired, 7: carer: looking after relative family)
ddply(mydata0910,.(dum_em13_46),nrow)
## prepare dummy for HHtype, HHtype2 = 1 if HHtype = 2, otherwise 0; HHtype3 = 1 if HHtype = 3, otherwise 0;
mydata0910["dum_HHtype2"]=NA
mydata0910=within(mydata0910,dum_HHtype2[HHtype==2]<-1)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
mydata0910=within(mydata0910,dum_HHtype2[HHtype==1]<-0)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
mydata0910=within(mydata0910,dum_HHtype2[HHtype==3]<-0)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
ddply(mydata0910,.(dum_HHtype2),nrow)
mydata0910["dum_HHtype3"]=NA
mydata0910=within(mydata0910,dum_HHtype3[HHtype==3]<-1)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
mydata0910=within(mydata0910,dum_HHtype3[HHtype==1]<-0)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
mydata0910=within(mydata0910,dum_HHtype3[HHtype==2]<-0)## 0 for employed people: 1: employee, 2:sel-employed with employees, 3:self-employed without employees;
## for 1 RE only: Extract random effect varaince and percentage of ICC (intercorrelation between RE in intercept and residuals)
## command: as.data.frame(VarCorr(test)) gives variance-covariance of the model: as.data.frame(VarCorr(test))$vcov; std.error of the model: as.data.frame(VarCorr(test))$sdcor
RE1=as.data.frame(VarCorr(T2))$vcov[1]## variance of RE in the intercept "ID"
RE2=as.data.frame(VarCorr(T2))$vcov[2]## varaince of residual ##
testlmer=as.vector(T2@pp$X%*%fixef(T2))## fitted values ## length of fitted values is 4099, no adjustment is made
VarF=var(testlmer)
## VarV = variance (intercept) + varaince(Slope) + 2 cov.correlation * stdev(intercept)stdev(slope) ## variation generated from RE