# analyze survey data for free (http://asdfree.com) with the r language
# european social survey
# 2010 examples

# # # # # # # # # # # # # # # # #
# # block of code to run this # #
# # # # # # # # # # # # # # # # #
# library(downloader)
# setwd( "..." )
# source_url( "https://raw.github.com/ajdamico/usgsd/master/European%20Social%20Survey/analysis%20examples.R",
#             prompt=FALSE,echo=TRUE )
# # # # # # # # # # # # # # #
# # end of auto-run block # #
# # # # # # # # # # # # # # #

# if you have never used the r language before,
# watch this two minute video i made outlining
# how to run this script from start to finish
# http://www.screenr.com/Zpd8

# daniel oberski
# daniel.oberski@gmail.com

# anthony joseph damico
# ajdamico@gmail.com

# if you use this script for a project, please send me a note
# it's always nice to hear about how people are using this stuff

# for further reading on cross-package comparisons, see:
# http://journal.r-project.org/archive/2009-2/RJournal_2009-2_Damico.pdf


#######################################################################
# analyze the 2010 European Social Survey lithuanian data file with R #
#######################################################################


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#######################################################################################################################
# prior to running this replication script, the 2010 ESS microdata files must be loaded as R data files (.rda)        #
# on the local machine. running the "download all microdata.R" script will create this file for you with zero hassle. #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# https://raw.github.com/ajdamico/usgsd/master/European%20Social%20Survey/download%20all%20microdata.R                #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# that script will save a number of .rda files in C:/My Directory/ESS/ (or the working directory was chosen)          #
#######################################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


# set your working directory.
# all ESS data files will be stored here
# after downloading and importing.
# use forward slashes instead of back slashes

# input directory where your ESS data was downloaded..
setwd( "/.../asdfree.com/European Social Survey/data/" )
dir()
## [1] "2002" "2004" "2006" "2008" "2010" "2012"
# ..in order to set your current working directory

# install.packages( c( 'survey' , 'downloader' ) )

library(survey)     # load survey package (analyzes complex design surveys)
## Loading required package: grid
## 
## Attaching package: 'survey'
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
library(downloader) # downloads and then runs the source() function on scripts from github

# by default, R will crash if a primary sampling unit (psu) has a single observation
# set R to produce conservative standard errors instead of crashing
# by uncommenting this line:
options( survey.lonely.psu = "adjust" ) # lithuania's round five data has some psu with single observations
# this setting matches the MISSUNIT option in SUDAAN
## read more: http://r-survey.r-forge.r-project.org/survey/exmample-lonely.html
### Lonely PSUs
## Variance estimation in sample surveys involves variances computed within primary sampling units. 
## If only one PSU is sampled from a particular stratum the variance can't be computed (there is 
## no unbiased estimator and the standard estimator gives 0/0).
### Certainty PSUs
## One exception to this is "certainty" PSUs in sampling without replacement, where the population 
## has only one PSU in the stratum. With 100% sampling, there is no contribution to the variance 
## from the first stage of sampling in this stratum. The easiest way to tell R you have certainty 
## PSUs is to use the fpc argument to svydesign.
### Other lonely PSUs
## More generally, some sort of ad hoc adjustment is needed. The best adjustment is probably 
## to combine the single-PSU stratum with another well-chosen stratum, but there are some fully 
## automatic adjustments available. The form of the adjustment is controlled by a global option. 
## The default is
## options(survey.lonely.psu="fail")
## which makes it an error to have a stratum with a single, non-certainty PSU.
### With
## options(survey.lonely.psu="remove")
## or
## options(survey.lonely.psu="certainty")
## a single-PSU stratum makes no contribution to the variance (for multistage sampling it makes 
## no contribution at that level of sampling). This is an alternative to specifying fpc, and 
## might be useful for compatibility with other software.
### With
## options(survey.lonely.psu="adjust")
## the data for the single-PSU stratum are centered at the sample grand mean rather than the 
## stratum mean. This is conservative.
### With
## options(survey.lonely.psu="average")
## the stratum contribution to the variance is taken to be the average of all the strata with 
## more than one PSU. This might be appropriate if the lonely PSUs were due to data missing at 
## random rather than to design deficiencies.
## Difficulties in estimating variances also arise when only one PSU in a stratum has observations 
## in a particular domain or subpopulation. R gives a warning rather than an error when this occurs, 
## and can optionally apply the "adjust" and "average" corrections. To apply the corrections, set
## options(survey.adjust.domain.lonely=TRUE)
## and set options("survey.lonely.psu") to the adjustment method you want to use. 


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# note about using country-specific data rather than the `integrated` multi-country files:  #
# the integrated files produced by the ESS data administrators do not include design info.  #
# that's fine if you don't care about standard errors or confidence intervals, but ...      #
# you should.                                                                               #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


# load lithuania's round five main and supplementary data file (ms)..
load( "./2010/LT/ESS5.rda" )
# ..and immediately save it to a more appropriately-named object
ess5.lt.ms <- x
# see a list of variables contained in the main and supplementary data file (ms)
# further information on question formulations, variables and their names and values
# is available at www.europeansocialsurvey.org/data/country.html?c=lithuania
names(ess5.lt.ms)
##   [1] "name"     "essround" "edition"  "proddate" "idno"     "cntry"   
##   [7] "tvtot"    "tvpol"    "rdtot"    "rdpol"    "nwsptot"  "nwsppol" 
##  [13] "netuse"   "ppltrst"  "pplfair"  "pplhlp"   "polintr"  "trstprl" 
##  [19] "trstlgl"  "trstplc"  "trstplt"  "trstprt"  "trstep"   "trstun"  
##  [25] "vote"     "prtvtcbe" "prtvtbbg" "prtvtcch" "prtvthr"  "prtvtcy" 
##  [31] "prtvtbcz" "prtvcde1" "prtvcde2" "prtvtbdk" "prtvtcee" "prtvtbes"
##  [37] "prtvtbfi" "prtvtbfr" "prtvtgb"  "prtvtcgr" "prtvtchu" "prtvtaie"
##  [43] "prtvtbil" "prtvlt1"  "prtvlt2"  "prtvlt3"  "prtvtdnl" "prtvtano"
##  [49] "prtvtbpl" "prtvtbpt" "prtvtbru" "prtvtase" "prtvtcsi" "prtvtbsk"
##  [55] "prtvtbua" "contplt"  "wrkprty"  "wrkorg"   "badge"    "sgnptit" 
##  [61] "pbldmn"   "bctprd"   "clsprty"  "prtclcbe" "prtclbbg" "prtclcch"
##  [67] "prtclcy"  "prtclhr"  "prtclbcz" "prtclcde" "prtclbdk" "prtclcee"
##  [73] "prtclbes" "prtclbfi" "prtclcfr" "prtclgb"  "prtclcgr" "prtclchu"
##  [79] "prtclaie" "prtclbil" "prtcllt"  "prtclcnl" "prtclano" "prtcldpl"
##  [85] "prtclcpt" "prtclbru" "prtclase" "prtclcsi" "prtclbsk" "prtclcua"
##  [91] "prtdgcl"  "mmbprty"  "prtmbcbe" "prtmbbbg" "prtmbcch" "prtmbhr" 
##  [97] "prtmbcy"  "prtmbbcz" "prtmbcde" "prtmbbdk" "prtmbcee" "prtmbbes"
## [103] "prtmbbfi" "prtmbcfr" "prtmbgb"  "prtmbcgr" "prtmbchu" "prtmbaie"
## [109] "prtmbbil" "prtmblt"  "prtmbcnl" "prtmbano" "prtmbdpl" "prtmbbpt"
## [115] "prtmbbru" "prtmbase" "prtmbcsi" "prtmbbsk" "prtmbcua" "lrscale" 
## [121] "stflife"  "stfeco"   "stfgov"   "stfdem"   "stfedu"   "stfhlth" 
## [127] "gincdif"  "freehms"  "prtyban"  "scnsenv"  "imsmetn"  "imdfetn" 
## [133] "impcntr"  "imbgeco"  "imueclt"  "imwbcnt"  "happy"    "sclmeet" 
## [139] "inmdisc"  "sclact"   "crmvct"   "aesfdrk"  "brghmwr"  "brghmef" 
## [145] "crvctwr"  "crvctef"  "health"   "hlthhmp"  "rlgblg"   "rlgdnm"  
## [151] "rlgdnbe"  "rlgdnach" "rlgdncy"  "rlgdnde"  "rlgdndk"  "rlgdnafi"
## [157] "rlgdngb"  "rlgdnagr" "rlgdnhu"  "rlgdnie"  "rlgdnil"  "rlgdnlt" 
## [163] "rlgdnnl"  "rlgdnno"  "rlgdnapl" "rlgdnpt"  "rlgdnaru" "rlgdnase"
## [169] "rlgdnsi"  "rlgdnsk"  "rlgdnua"  "rlgblge"  "rlgdnme"  "rlgdebe" 
## [175] "rlgdeach" "rlgdecy"  "rlgdede"  "rlgdedk"  "rlgdeafi" "rlgdegb" 
## [181] "rlgdeagr" "rlgdehu"  "rlgdeie"  "rlgdeil"  "rlgdelt"  "rlgdenl" 
## [187] "rlgdeno"  "rlgdeapl" "rlgdept"  "rlgdearu" "rlgdease" "rlgdesi" 
## [193] "rlgdesk"  "rlgdeua"  "rlgdgr"   "rlgatnd"  "pray"     "dscrgrp" 
## [199] "dscrrce"  "dscrntn"  "dscrrlg"  "dscrlng"  "dscretn"  "dscrage" 
## [205] "dscrgnd"  "dscrsex"  "dscrdsb"  "dscroth"  "dscrdk"   "dscrref" 
## [211] "dscrnap"  "dscrna"   "ctzcntr"  "ctzshipb" "brncntr"  "cntbrthb"
## [217] "livecnta" "lnghom1"  "lnghom2"  "blgetmg"  "facntr"   "fbrncnta"
## [223] "mocntr"   "mbrncnta" "insclwr"  "bystlwr"  "trfowr"   "insclct" 
## [229] "bystlct"  "trfoct"   "plcjbcn"  "plccont"  "plcstf"   "plcvcrp" 
## [235] "plcvcrc"  "plcpvcr"  "plccbrg"  "plcarcr"  "plcrspc"  "plcfrdc" 
## [241] "plcexdc"  "bplcdc"   "doplcsy"  "dpcstrb"  "plcrgwr"  "plcipvl" 
## [247] "gsupplc"  "plciplt"  "plccbrb"  "ctjbcnt"  "ctmtgfr"  "ctfrdc"  
## [253] "wraccrp"  "wraccrc"  "jdgcbrb"  "ctprpwr"  "hrshsnta" "dbctvrd" 
## [259] "lwstrob"  "rgbrklw"  "ctinplt"  "stcbg2t"  "tmprs"    "caplcst" 
## [265] "widprsn"  "wevdct"   "flsin5y"  "bstln5y"  "troff5y"  "hhmmb"   
## [271] "gndr"     "gndr2"    "gndr3"    "gndr4"    "gndr5"    "gndr6"   
## [277] "gndr7"    "gndr8"    "gndr9"    "gndr10"   "gndr11"   "gndr12"  
## [283] "gndr13"   "gndr14"   "yrbrn"    "agea"     "yrbrn2"   "yrbrn3"  
## [289] "yrbrn4"   "yrbrn5"   "yrbrn6"   "yrbrn7"   "yrbrn8"   "yrbrn9"  
## [295] "yrbrn10"  "yrbrn11"  "yrbrn12"  "yrbrn13"  "yrbrn14"  "rshipa2" 
## [301] "rshipa3"  "rshipa4"  "rshipa5"  "rshipa6"  "rshipa7"  "rshipa8" 
## [307] "rshipa9"  "rshipa10" "rshipa11" "rshipa12" "rshipa13" "rshipa14"
## [313] "icpart1"  "rshpsts"  "lvgptnea" "dvrcdeva" "icpart2"  "iccohbt" 
## [319] "marsts"   "marstie"  "maritalb" "chldhm"   "chldhhe"  "fxltph"  
## [325] "domicil"  "edulvlb"  "eisced"   "edlvdbe"  "edlvdbg"  "edlvdch" 
## [331] "edlvdhr"  "edlvdcy"  "edlvdcz"  "edude1"   "edude2"   "edude3"  
## [337] "edlvddk"  "edlvdee"  "edlvdes"  "edlvdfi"  "edlvdfr"  "edugb1"  
## [343] "edugb2"   "edagegb"  "edlvdgr"  "edlvdhu"  "edlvdie"  "eduil1"  
## [349] "eduil2"   "edlvdlt"  "edlvdnl"  "edlvdno"  "edlvdpl"  "eduyrpl" 
## [355] "edupl2"   "edlvdpt"  "edlvdru"  "edlvdse"  "edlvdsi"  "edlvdsk" 
## [361] "edlvdua"  "eduyrs"   "pdwrk"    "edctn"    "uempla"   "uempli"  
## [367] "dsbld"    "rtrd"     "cmsrv"    "hswrk"    "dngoth"   "dngdk"   
## [373] "dngref"   "dngna"    "mainact"  "mnactic"  "crpdwk"   "pdjobev" 
## [379] "pdjobyr"  "emplrel"  "emplno"   "wrkctra"  "jbtmppm"  "estsz"   
## [385] "jbspv"    "njbspv"   "wkdcorga" "iorgact"  "wkdcpce"  "wkhct"   
## [391] "wkhtot"   "nacer2"   "tporgwk"  "iscoco"   "rsnlvem"  "wrkac6m" 
## [397] "uemp3m"   "uemp12m"  "uemp5yr"  "mbtru"    "hincsrca" "hinctnta"
## [403] "pphincr"  "hincfel"  "brwmny"   "icpart3"  "edulvlpb" "eiscedp" 
## [409] "edlvpdbe" "edlvpdbg" "edlvpdch" "edlvpdhr" "edlvpdcy" "edlvpdcz"
## [415] "edupde1"  "edupde2"  "edupde3"  "edlvpddk" "edlvpdee" "edlvpdes"
## [421] "edlvpdfi" "edlvpdfr" "edupgb1"  "edupgb2"  "edagepgb" "edlvpdgr"
## [427] "edlvpdhu" "edlvpdie" "edupil1"  "edupil2"  "edlvpdlt" "edlvpdnl"
## [433] "edlvpdno" "edlvpdpl" "eduyrppl" "eduppl2"  "edlvpdpt" "edlvpdru"
## [439] "edlvpdse" "edlvpdsi" "edlvpdsk" "edlvpdua" "pdwrkp"   "edctnp"  
## [445] "uemplap"  "uemplip"  "dsbldp"   "rtrdp"    "cmsrvp"   "hswrkp"  
## [451] "dngothp"  "dngdkp"   "dngnapp"  "dngrefp"  "dngnap"   "mnactp"  
## [457] "crpdwkp"  "iscocop"  "emprelp"  "emplnop"  "jbspvp"   "njbspvp" 
## [463] "wkhtotp"  "edulvlfb" "eiscedf"  "edlvfdbe" "edlvfdbg" "edlvfdch"
## [469] "edlvfdhr" "edlvfdcy" "edlvfdcz" "edufde1"  "edufde2"  "edufde3" 
## [475] "edlvfddk" "edlvfdee" "edlvfdes" "edlvfdfi" "edlvfdfr" "edufgb1" 
## [481] "edufgb2"  "edagefgb" "edlvfdgr" "edlvfdhu" "edlvfdie" "edufil1" 
## [487] "edufil2"  "edlvfdlt" "edlvfdnl" "edlvfdno" "edlvfdpl" "edlvfdpt"
## [493] "edlvfdru" "edlvfdse" "edlvfdsi" "edlvfdsk" "edlvfdua" "emprf14" 
## [499] "emplnof"  "jbspvf"   "occf14b"  "edulvlmb" "eiscedm"  "edlvmdbe"
## [505] "edlvmdbg" "edlvmdch" "edlvmdhr" "edlvmdcy" "edlvmdcz" "edumde1" 
## [511] "edumde2"  "edumde3"  "edlvmddk" "edlvmdee" "edlvmdes" "edlvmdfi"
## [517] "edlvmdfr" "edumgb1"  "edumgb2"  "edagemgb" "edlvmdgr" "edlvmdhu"
## [523] "edlvmdie" "edumil1"  "edumil2"  "edlvmdlt" "edlvmdnl" "edlvmdno"
## [529] "edlvmdpl" "edlvmdpt" "edlvmdru" "edlvmdse" "edlvmdsi" "edlvmdsk"
## [535] "edlvmdua" "emprm14"  "emplnom"  "jbspvm"   "occm14b"  "atncrse" 
## [541] "edul12m"  "useknwl"  "edupdem"  "gdsprt"   "clmrlx"   "actvgrs" 
## [547] "wmcpwrk"  "mnrgtjb"  "gvprppv"  "fltlnla"  "mlohinc"  "dsdclve" 
## [553] "cuthheq"  "yrspdwka" "icmnact"  "jbtsktm"  "yrskdwk"  "wrkengt" 
## [559] "wkovrtm"  "wrkwe"    "yrcremp"  "emplrjb"  "mnrsefw"  "scrsefw" 
## [565] "jbcoedu"  "jbedyrs"  "jblrn"    "vrtywrk"  "jbrqlrn"  "wgdpeft" 
## [571] "hlpcowk"  "hlthrwk"  "dcsfwrk"  "jbscr"    "lsintjb"  "wrkhrd"  
## [577] "nevdnjb"  "oprtad"   "bsmw"     "ppwwkp"   "bseftwk"  "smblvjb" 
## [583] "rpljbde"  "wpmwkcp"  "indisdc"  "truinwk"  "pdaprpa"  "wrywprb" 
## [589] "trdawrk"  "jbprtfp"  "icnopfma" "pfmfdjba" "frsptjb"  "dfcnswka"
## [595] "stfmjob"  "stfjbot"  "wkjbndm"  "grspaya"  "payprda"  "linwk3y" 
## [601] "rdpay3y"  "wkshr3y"  "lscjb3y"  "orgfd3y"  "npemp3y"  "icb1940" 
## [607] "ipjbini"  "ipjbscr"  "ipjbhin"  "ipjbwfm"  "ipjbtro"  "pdjbndm" 
## [613] "uemp3y"   "wkhsch"   "icptn"    "pwkhsch"  "puemp3y"  "hwwkhs"  
## [619] "phwwkhs"  "dsgrmnya" "icptnwka" "wkengtp"  "wkovtmp"  "ptnwkwe" 
## [625] "icmnart"  "rtryr"    "wntrtr"   "icb1964"  "agertr"   "plnchld" 
## [631] "ipcrtiv"  "imprich"  "ipeqopt"  "ipshabt"  "impsafe"  "impdiff" 
## [637] "ipfrule"  "ipudrst"  "ipmodst"  "ipgdtim"  "impfree"  "iphlppl" 
## [643] "ipsuces"  "ipstrgv"  "ipadvnt"  "ipbhprp"  "iprspot"  "iplylfr" 
## [649] "impenv"   "imptrad"  "impfun"   "region"   "regunit"  "intewde" 
## [655] "inwdds"   "inwmms"   "inwyys"   "inwshh"   "inwsmm"   "inwdde"  
## [661] "inwmme"   "inwyye"   "inwehh"   "inwemm"   "inwtm"    "spltadmd"
## [667] "supqad1"  "supqad2"  "supqdd"   "supqmm"   "supqyr"   "dweight" 
## [673] "pspwght"  "pweight"
# load belgium's round five sample design data file (sddf)..
load( "./2010/LT/ESS5__SDDF.rda" )
# ..and immediately save it to a more appropriately-named object
ess5.lt.sddf <- x
# see summary statistics for all variables contained in the sample design data file (sddf) 
summary(ess5.lt.sddf)
##  cntry          idno            psu           samppoin   
##  LT:1681   Min.   :    1   Min.   :  1.0   Min.   : NA   
##            1st Qu.:  422   1st Qu.: 47.0   1st Qu.: NA   
##            Median :  842   Median : 96.0   Median : NA   
##            Mean   : 1263   Mean   : 97.7   Mean   :NaN   
##            3rd Qu.: 1262   3rd Qu.:146.0   3rd Qu.: NA   
##            Max.   :13351   Max.   :200.0   Max.   : NA   
##                                            NA's   :1681  
##                            stratify         prob        
##  162                           :  24   Min.   :0.00004  
##  45                            :  20   1st Qu.:0.00100  
##  97                            :  20   Median :0.00248  
##  163                           :  19   Mean   :0.00749  
##  197                           :  19   3rd Qu.:0.00744  
##  89                            :  18   Max.   :0.17388  
##  (Other)                       :1561
# note that the sample design data files (sddf) do not get released
# at the same time as the main data file, for some odd reason.
# since that prevents you from knowing how good your estimates actually are,
# it's an incomplete analysis.  better to wait for the full data.
# survey research has two components:
# knowing something (the coefficient) and
# understanding how well you know it (the variance)

# merge these two files together, creating a merged object..
# warning: there are more cases in sddf file than in ms file due to 4 cases being incomplete 
summary(ess5.lt.ms$cntry); summary(ess5.lt.sddf$cntry)
##   LT 
## 1677
##   LT 
## 1681
# and removed from ms file; therefore, we need to remove these cases from the sddf file, too.
ess5.lt.sddf <- ess5.lt.sddf[match(ess5.lt.ms$idno,ess5.lt.sddf$idno),]
# ..do the merging..
ess5.lt <- merge( ess5.lt.ms , ess5.lt.sddf , by=c("cntry", "idno") , all = TRUE )
# ..and immediately check that all record counts match up
stopifnot( nrow( ess5.lt.ms ) == nrow( ess5.lt ) & nrow( ess5.lt.sddf ) == nrow( ess5.lt ) )
# more information on merging data files in r: http://dss.princeton.edu/training/Merge101R.pdf

# display the number of rows in the cross-sectional cumulative data set
nrow( ess5.lt )
## [1] 1677
# display the first six records in the cross-sectional cumulative data set
head( ess5.lt )
##   cntry idno         name essround edition   proddate tvtot tvpol rdtot
## 1    LT    1 ESS5e03_1           5     3.1 29.04.2014     7     4    NA
## 2    LT   10 ESS5e03_1           5     3.1 29.04.2014     1     0     0
## 3    LT  100 ESS5e03_1           5     3.1 29.04.2014     7     1     0
## 4    LT 1000 ESS5e03_1           5     3.1 29.04.2014     6     2     2
## 5    LT 1001 ESS5e03_1           5     3.1 29.04.2014     5     2     0
## 6    LT 1002 ESS5e03_1           5     3.1 29.04.2014     4     1     2
##   rdpol nwsptot nwsppol netuse ppltrst pplfair pplhlp polintr trstprl
## 1     0      NA      NA      0       5       5      5       3       1
## 2    NA       2       0      7       3       5     10       2       0
## 3    NA       1       1      0       3       7      2       3       0
## 4     1       1       0      0       7       7      6       3       1
## 5    NA       1       1      0       6       6      5       3       1
## 6     1       1       1      6       6       6      4       4       0
##   trstlgl trstplc trstplt trstprt trstep trstun vote prtvtcbe prtvtbbg
## 1       1       8       1       1      1     NA    1       NA       NA
## 2       6      10       0       5      0      5    1       NA       NA
## 3      NA       3      NA      NA     NA     NA    1       NA       NA
## 4       1       1       1       1      1      1    2       NA       NA
## 5       1       1       1       1      4      4    1       NA       NA
## 6       0       1       0       0      2      2    2       NA       NA
##   prtvtcch prtvthr prtvtcy prtvtbcz prtvcde1 prtvcde2 prtvtbdk prtvtcee
## 1       NA      NA      NA       NA       NA       NA       NA       NA
## 2       NA      NA      NA       NA       NA       NA       NA       NA
## 3       NA      NA      NA       NA       NA       NA       NA       NA
## 4       NA      NA      NA       NA       NA       NA       NA       NA
## 5       NA      NA      NA       NA       NA       NA       NA       NA
## 6       NA      NA      NA       NA       NA       NA       NA       NA
##   prtvtbes prtvtbfi prtvtbfr prtvtgb prtvtcgr prtvtchu prtvtaie prtvtbil
## 1       NA       NA       NA      NA       NA       NA       NA       NA
## 2       NA       NA       NA      NA       NA       NA       NA       NA
## 3       NA       NA       NA      NA       NA       NA       NA       NA
## 4       NA       NA       NA      NA       NA       NA       NA       NA
## 5       NA       NA       NA      NA       NA       NA       NA       NA
## 6       NA       NA       NA      NA       NA       NA       NA       NA
##   prtvlt1 prtvlt2 prtvlt3 prtvtdnl prtvtano prtvtbpl prtvtbpt prtvtbru
## 1      NA      NA      NA       NA       NA       NA       NA       NA
## 2      10      10      55       NA       NA       NA       NA       NA
## 3      NA      NA      44       NA       NA       NA       NA       NA
## 4      NA      NA      NA       NA       NA       NA       NA       NA
## 5       1      44      44       NA       NA       NA       NA       NA
## 6      NA      NA      NA       NA       NA       NA       NA       NA
##   prtvtase prtvtcsi prtvtbsk prtvtbua contplt wrkprty wrkorg badge sgnptit
## 1       NA       NA       NA       NA       2       2      2     2       2
## 2       NA       NA       NA       NA       2       2      2     2       2
## 3       NA       NA       NA       NA       2       2      2     2       2
## 4       NA       NA       NA       NA       2       2      2     2       2
## 5       NA       NA       NA       NA       2       2      2     2       2
## 6       NA       NA       NA       NA       2       2      2     2       2
##   pbldmn bctprd clsprty prtclcbe prtclbbg prtclcch prtclcy prtclhr
## 1      2      2      NA       NA       NA       NA      NA      NA
## 2      2      2       2       NA       NA       NA      NA      NA
## 3      2      2       2       NA       NA       NA      NA      NA
## 4      2      2       2       NA       NA       NA      NA      NA
## 5      2      2       2       NA       NA       NA      NA      NA
## 6      2      2       2       NA       NA       NA      NA      NA
##   prtclbcz prtclcde prtclbdk prtclcee prtclbes prtclbfi prtclcfr prtclgb
## 1       NA       NA       NA       NA       NA       NA       NA      NA
## 2       NA       NA       NA       NA       NA       NA       NA      NA
## 3       NA       NA       NA       NA       NA       NA       NA      NA
## 4       NA       NA       NA       NA       NA       NA       NA      NA
## 5       NA       NA       NA       NA       NA       NA       NA      NA
## 6       NA       NA       NA       NA       NA       NA       NA      NA
##   prtclcgr prtclchu prtclaie prtclbil prtcllt prtclcnl prtclano prtcldpl
## 1       NA       NA       NA       NA      NA       NA       NA       NA
## 2       NA       NA       NA       NA      NA       NA       NA       NA
## 3       NA       NA       NA       NA      NA       NA       NA       NA
## 4       NA       NA       NA       NA      NA       NA       NA       NA
## 5       NA       NA       NA       NA      NA       NA       NA       NA
## 6       NA       NA       NA       NA      NA       NA       NA       NA
##   prtclcpt prtclbru prtclase prtclcsi prtclbsk prtclcua prtdgcl mmbprty
## 1       NA       NA       NA       NA       NA       NA      NA       2
## 2       NA       NA       NA       NA       NA       NA      NA       2
## 3       NA       NA       NA       NA       NA       NA      NA       2
## 4       NA       NA       NA       NA       NA       NA      NA       2
## 5       NA       NA       NA       NA       NA       NA      NA       2
## 6       NA       NA       NA       NA       NA       NA      NA       2
##   prtmbcbe prtmbbbg prtmbcch prtmbhr prtmbcy prtmbbcz prtmbcde prtmbbdk
## 1       NA       NA       NA      NA      NA       NA       NA       NA
## 2       NA       NA       NA      NA      NA       NA       NA       NA
## 3       NA       NA       NA      NA      NA       NA       NA       NA
## 4       NA       NA       NA      NA      NA       NA       NA       NA
## 5       NA       NA       NA      NA      NA       NA       NA       NA
## 6       NA       NA       NA      NA      NA       NA       NA       NA
##   prtmbcee prtmbbes prtmbbfi prtmbcfr prtmbgb prtmbcgr prtmbchu prtmbaie
## 1       NA       NA       NA       NA      NA       NA       NA       NA
## 2       NA       NA       NA       NA      NA       NA       NA       NA
## 3       NA       NA       NA       NA      NA       NA       NA       NA
## 4       NA       NA       NA       NA      NA       NA       NA       NA
## 5       NA       NA       NA       NA      NA       NA       NA       NA
## 6       NA       NA       NA       NA      NA       NA       NA       NA
##   prtmbbil prtmblt prtmbcnl prtmbano prtmbdpl prtmbbpt prtmbbru prtmbase
## 1       NA      NA       NA       NA       NA       NA       NA       NA
## 2       NA      NA       NA       NA       NA       NA       NA       NA
## 3       NA      NA       NA       NA       NA       NA       NA       NA
## 4       NA      NA       NA       NA       NA       NA       NA       NA
## 5       NA      NA       NA       NA       NA       NA       NA       NA
## 6       NA      NA       NA       NA       NA       NA       NA       NA
##   prtmbcsi prtmbbsk prtmbcua lrscale stflife stfeco stfgov stfdem stfedu
## 1       NA       NA       NA      NA       2      2     NA      2     NA
## 2       NA       NA       NA       5       9      6      3      0     10
## 3       NA       NA       NA      NA       5      1      1      1      5
## 4       NA       NA       NA       5       3      1      1      1      7
## 5       NA       NA       NA       5       5      0      1      1      5
## 6       NA       NA       NA       5       3      0      1      1      3
##   stfhlth gincdif freehms prtyban scnsenv imsmetn imdfetn impcntr imbgeco
## 1       5       1       3       3       3       4       4       4       2
## 2       7       2       5       2       1       1       1       1       5
## 3       5       1       5      NA       2       1       1       1       3
## 4       5       2       4       2       4       2       2       2       5
## 5       5       2       4       2       4       3       3       3       4
## 6       1       2       4       4       4       2       2       2       3
##   imueclt imwbcnt happy sclmeet inmdisc sclact crmvct aesfdrk brghmwr
## 1       5       2     2       1       2      2      2       4       2
## 2      NA      NA     5       3       1      2      2       2       4
## 3       1       0     5       2       1      1      2       2       4
## 4       6       5     4       4       1      3      2       3       4
## 5       5       5     5       3       1      3      2       3       4
## 6       6       5     3       2       2      2      2       3       4
##   brghmef crvctwr crvctef health hlthhmp rlgblg rlgdnm rlgdnbe rlgdnach
## 1       2       2       2      5       1      1      1      NA       NA
## 2      NA       3       3      1       3      1      1      NA       NA
## 3      NA       4      NA      2       3      1      1      NA       NA
## 4      NA       4      NA      2       3      1      1      NA       NA
## 5      NA       4      NA      2       3      1      1      NA       NA
## 6      NA       4      NA      3       3      2     NA      NA       NA
##   rlgdncy rlgdnde rlgdndk rlgdnafi rlgdngb rlgdnagr rlgdnhu rlgdnie
## 1      NA      NA      NA       NA      NA       NA      NA      NA
## 2      NA      NA      NA       NA      NA       NA      NA      NA
## 3      NA      NA      NA       NA      NA       NA      NA      NA
## 4      NA      NA      NA       NA      NA       NA      NA      NA
## 5      NA      NA      NA       NA      NA       NA      NA      NA
## 6      NA      NA      NA       NA      NA       NA      NA      NA
##   rlgdnil rlgdnlt rlgdnnl rlgdnno rlgdnapl rlgdnpt rlgdnaru rlgdnase
## 1      NA       1      NA      NA       NA      NA       NA       NA
## 2      NA       1      NA      NA       NA      NA       NA       NA
## 3      NA       1      NA      NA       NA      NA       NA       NA
## 4      NA       1      NA      NA       NA      NA       NA       NA
## 5      NA       1      NA      NA       NA      NA       NA       NA
## 6      NA      NA      NA      NA       NA      NA       NA       NA
##   rlgdnsi rlgdnsk rlgdnua rlgblge rlgdnme rlgdebe rlgdeach rlgdecy rlgdede
## 1      NA      NA      NA      NA      NA      NA       NA      NA      NA
## 2      NA      NA      NA      NA      NA      NA       NA      NA      NA
## 3      NA      NA      NA      NA      NA      NA       NA      NA      NA
## 4      NA      NA      NA      NA      NA      NA       NA      NA      NA
## 5      NA      NA      NA      NA      NA      NA       NA      NA      NA
## 6      NA      NA      NA       2      NA      NA       NA      NA      NA
##   rlgdedk rlgdeafi rlgdegb rlgdeagr rlgdehu rlgdeie rlgdeil rlgdelt
## 1      NA       NA      NA       NA      NA      NA      NA      NA
## 2      NA       NA      NA       NA      NA      NA      NA      NA
## 3      NA       NA      NA       NA      NA      NA      NA      NA
## 4      NA       NA      NA       NA      NA      NA      NA      NA
## 5      NA       NA      NA       NA      NA      NA      NA      NA
## 6      NA       NA      NA       NA      NA      NA      NA      NA
##   rlgdenl rlgdeno rlgdeapl rlgdept rlgdearu rlgdease rlgdesi rlgdesk
## 1      NA      NA       NA      NA       NA       NA      NA      NA
## 2      NA      NA       NA      NA       NA       NA      NA      NA
## 3      NA      NA       NA      NA       NA       NA      NA      NA
## 4      NA      NA       NA      NA       NA       NA      NA      NA
## 5      NA      NA       NA      NA       NA       NA      NA      NA
## 6      NA      NA       NA      NA       NA       NA      NA      NA
##   rlgdeua rlgdgr rlgatnd pray dscrgrp dscrrce dscrntn dscrrlg dscrlng
## 1      NA     NA       5    1       2       0       0       0       0
## 2      NA      5       5    7       2       0       0       0       0
## 3      NA     10       3    3       2       0       0       0       0
## 4      NA      6       6    7       2       0       0       0       0
## 5      NA      6       6    7       2       0       0       0       0
## 6      NA      2       6    7       2       0       0       0       0
##   dscretn dscrage dscrgnd dscrsex dscrdsb dscroth dscrdk dscrref dscrnap
## 1       0       0       0       0       0       0      0       0       1
## 2       0       0       0       0       0       0      0       0       1
## 3       0       0       0       0       0       0      0       0       1
## 4       0       0       0       0       0       0      0       0       1
## 5       0       0       0       0       0       0      0       0       1
## 6       0       0       0       0       0       0      0       0       1
##   dscrna ctzcntr ctzshipb brncntr cntbrthb livecnta lnghom1 lnghom2
## 1      0       1     <NA>       1     <NA>       NA     LIT    <NA>
## 2      0       1     <NA>       1     <NA>       NA     LIT    <NA>
## 3      0       1     <NA>       1     <NA>       NA     LIT    <NA>
## 4      0       1     <NA>       1     <NA>       NA     LIT    <NA>
## 5      0       1     <NA>       1     <NA>       NA     LIT    <NA>
## 6      0       1     <NA>       1     <NA>       NA     LIT    <NA>
##   blgetmg facntr fbrncnta mocntr mbrncnta insclwr bystlwr trfowr insclct
## 1       2      1     <NA>      1     <NA>       4       4      4       3
## 2       2      1     <NA>      1     <NA>       3       3      2       3
## 3       2      1     <NA>      1     <NA>      NA       4      4      NA
## 4       2      1     <NA>      1     <NA>       3       3      3       3
## 5       2      1     <NA>      1     <NA>       3       3      3       1
## 6       2      1     <NA>      1     <NA>       3       3      3       1
##   bystlct trfoct plcjbcn plccont plcstf plcvcrp plcvcrc plcpvcr plccbrg
## 1       3      3       3       2     NA      NA      NA       4       4
## 2       2      3       2       2     NA       2       1       8      10
## 3       3      3       3       2     NA       2      NA      NA      NA
## 4       2      3       4       2     NA       2       3       3       5
## 5       3      3       4       2     NA       2       3       2       2
## 6       2      2       4       2     NA       2      NA       3       3
##   plcarcr plcrspc plcfrdc plcexdc bplcdc doplcsy dpcstrb plcrgwr plcipvl
## 1       3      NA       3       2      6       7       8       2       2
## 2       8       4      NA       2      5      NA      10       4       3
## 3      NA      NA      NA      NA      2       9       6       2       2
## 4       5       3       3       2      7       4       3       3       2
## 5       2       3       2       2      6       5       6       3       2
## 6       4       2       2       2      5       4       6       4       3
##   gsupplc plciplt plccbrb ctjbcnt ctmtgfr ctfrdc wraccrp wraccrc jdgcbrb
## 1       3       3      NA       3       5      5       2      NA       6
## 2       4       4       5       3       5     NA       2       3       5
## 3       2      NA      NA      NA       6      5       2      NA       8
## 4       2       2       7       4       7      3       2       3       7
## 5       4       2       7       4       7      3       2       3       7
## 6       3       2       7       4       7      3       2       3       7
##   ctprpwr hrshsnta dbctvrd lwstrob rgbrklw ctinplt stcbg2t tmprs caplcst
## 1       2        3       2       2       2       2       5    NA       3
## 2       2        2       2       2       2       2       2    NA       2
## 3       2        2       2       2       2      NA       4    NA       3
## 4       2        2       2       2       4       3       4    NA       3
## 5       2        2       2       2       4       2       2    NA       3
## 6       2        2       3       2       4       2       2    NA       3
##   widprsn wevdct flsin5y bstln5y troff5y hhmmb gndr gndr2 gndr3 gndr4
## 1       2      2       1       1       1     1    2    NA    NA    NA
## 2       1      1       1       1       1     5    1     1     2     1
## 3       2      2      NA       1       1     1    2    NA    NA    NA
## 4       2      2       1       1       1     1    1    NA    NA    NA
## 5       2      2       1       1       2     2    1     2    NA    NA
## 6       2      2       1       1       1     2    2     1    NA    NA
##   gndr5 gndr6 gndr7 gndr8 gndr9 gndr10 gndr11 gndr12 gndr13 gndr14 yrbrn
## 1    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA  1932
## 2     2    NA    NA    NA    NA     NA     NA     NA     NA     NA  1990
## 3    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA  1959
## 4    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA  1964
## 5    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA    NA
## 6    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA  1959
##   agea yrbrn2 yrbrn3 yrbrn4 yrbrn5 yrbrn6 yrbrn7 yrbrn8 yrbrn9 yrbrn10
## 1   79     NA     NA     NA     NA     NA     NA     NA     NA      NA
## 2   21   1962   1961   1991   1928     NA     NA     NA     NA      NA
## 3   52     NA     NA     NA     NA     NA     NA     NA     NA      NA
## 4   47     NA     NA     NA     NA     NA     NA     NA     NA      NA
## 5   NA     NA     NA     NA     NA     NA     NA     NA     NA      NA
## 6   52   1955     NA     NA     NA     NA     NA     NA     NA      NA
##   yrbrn11 yrbrn12 yrbrn13 yrbrn14 rshipa2 rshipa3 rshipa4 rshipa5 rshipa6
## 1      NA      NA      NA      NA      NA      NA      NA      NA      NA
## 2      NA      NA      NA      NA       3       3       4       5      NA
## 3      NA      NA      NA      NA      NA      NA      NA      NA      NA
## 4      NA      NA      NA      NA      NA      NA      NA      NA      NA
## 5      NA      NA      NA      NA       1      NA      NA      NA      NA
## 6      NA      NA      NA      NA       1      NA      NA      NA      NA
##   rshipa7 rshipa8 rshipa9 rshipa10 rshipa11 rshipa12 rshipa13 rshipa14
## 1      NA      NA      NA       NA       NA       NA       NA       NA
## 2      NA      NA      NA       NA       NA       NA       NA       NA
## 3      NA      NA      NA       NA       NA       NA       NA       NA
## 4      NA      NA      NA       NA       NA       NA       NA       NA
## 5      NA      NA      NA       NA       NA       NA       NA       NA
## 6      NA      NA      NA       NA       NA       NA       NA       NA
##   icpart1 rshpsts lvgptnea dvrcdeva icpart2 iccohbt marsts marstie
## 1       2      NA        1        2       2      NA      5      NA
## 2       2      NA        2        2       2      NA      6      NA
## 3       2      NA        2        2       2      NA     NA      NA
## 4       2      NA        1        1       2      NA      4      NA
## 5       1       1        2        2       1       2     NA      NA
## 6       1       1        2        2       1       2     NA      NA
##   maritalb chldhm chldhhe fxltph domicil edulvlb eisced edlvdbe edlvdbg
## 1        5      2       1      2       4       0      1      NA      NA
## 2        6      2       2      2       3     423      5      NA      NA
## 3       NA      2       1      1       4     313      4      NA      NA
## 4        4      2       1      2       1     323      4      NA      NA
## 5        1      2       1      2       1     323      4      NA      NA
## 6        1      2       1      2       1     620      6      NA      NA
##   edlvdch edlvdhr edlvdcy edlvdcz edude1 edude2 edude3 edlvddk edlvdee
## 1      NA      NA      NA      NA     NA     NA     NA      NA      NA
## 2      NA      NA      NA      NA     NA     NA     NA      NA      NA
## 3      NA      NA      NA      NA     NA     NA     NA      NA      NA
## 4      NA      NA      NA      NA     NA     NA     NA      NA      NA
## 5      NA      NA      NA      NA     NA     NA     NA      NA      NA
## 6      NA      NA      NA      NA     NA     NA     NA      NA      NA
##   edlvdes edlvdfi edlvdfr edugb1 edugb2 edagegb edlvdgr edlvdhu edlvdie
## 1      NA      NA      NA     NA     NA      NA      NA      NA      NA
## 2      NA      NA      NA     NA     NA      NA      NA      NA      NA
## 3      NA      NA      NA     NA     NA      NA      NA      NA      NA
## 4      NA      NA      NA     NA     NA      NA      NA      NA      NA
## 5      NA      NA      NA     NA     NA      NA      NA      NA      NA
## 6      NA      NA      NA     NA     NA      NA      NA      NA      NA
##   eduil1 eduil2 edlvdlt edlvdnl edlvdno edlvdpl eduyrpl edupl2 edlvdpt
## 1     NA     NA       0      NA      NA      NA      NA     NA      NA
## 2     NA     NA       9      NA      NA      NA      NA     NA      NA
## 3     NA     NA       7      NA      NA      NA      NA     NA      NA
## 4     NA     NA       8      NA      NA      NA      NA     NA      NA
## 5     NA     NA       8      NA      NA      NA      NA     NA      NA
## 6     NA     NA      13      NA      NA      NA      NA     NA      NA
##   edlvdru edlvdse edlvdsi edlvdsk edlvdua eduyrs pdwrk edctn uempla uempli
## 1      NA      NA      NA      NA      NA      0     0     0      0      0
## 2      NA      NA      NA      NA      NA     13     0     1      0      0
## 3      NA      NA      NA      NA      NA     11     1     0      0      0
## 4      NA      NA      NA      NA      NA     11     1     0      0      0
## 5      NA      NA      NA      NA      NA     11     1     0      0      0
## 6      NA      NA      NA      NA      NA     16     1     0      0      0
##   dsbld rtrd cmsrv hswrk dngoth dngdk dngref dngna mainact mnactic crpdwk
## 1     0    0     0     1      0     0      0     0      NA       8      2
## 2     0    0     0     0      0     0      0     0      NA       2      2
## 3     0    0     0     0      0     0      0     0      NA       1     NA
## 4     0    0     0     0      0     0      0     0      NA       1     NA
## 5     0    0     0     0      0     0      0     0      NA       1     NA
## 6     0    0     0     0      0     0      0     0      NA       1     NA
##   pdjobev pdjobyr emplrel emplno wrkctra jbtmppm estsz jbspv njbspv
## 1       1    1982       1     NA       1      NA    NA     2     NA
## 2       1    2009       1     NA       2       1     3     2     NA
## 3      NA      NA       1     NA       1      NA     3     2     NA
## 4      NA      NA       1     NA       1      NA     3     2     NA
## 5      NA      NA       1     NA       1      NA     3     2     NA
## 6      NA      NA       1     NA       1      NA     2     2     NA
##   wkdcorga iorgact wkdcpce wkhct wkhtot nacer2 tporgwk iscoco rsnlvem
## 1        3       0       6    40     40      1       3   9211       8
## 2        0       0      10    58     58     10       4   8271       3
## 3        1       0       0    40     40     85       2   9132      11
## 4        0       0       0    40     40     49       4   7231      11
## 5        2       1       1    40     40     32       4   7212      11
## 6        3       1       2    40     40     85       3   2320      NA
##   wrkac6m uemp3m uemp12m uemp5yr mbtru hincsrca hinctnta pphincr hincfel
## 1       2      2      NA      NA     2        1        2       6       3
## 2       2      1       2       2     3        1        6       1       3
## 3       2      2      NA      NA     2        1        1       7       2
## 4       2      2      NA      NA     3        1        6       7       3
## 5       2      2      NA      NA     2        1        7       5       2
## 6      NA      2      NA      NA     3        1       NA       3       2
##   brwmny icpart3 edulvlpb eiscedp edlvpdbe edlvpdbg edlvpdch edlvpdhr
## 1      3       2       NA      NA       NA       NA       NA       NA
## 2      4       2       NA      NA       NA       NA       NA       NA
## 3      1       2       NA      NA       NA       NA       NA       NA
## 4      2       2       NA      NA       NA       NA       NA       NA
## 5      3       1      323       4       NA       NA       NA       NA
## 6      2       1      620       6       NA       NA       NA       NA
##   edlvpdcy edlvpdcz edupde1 edupde2 edupde3 edlvpddk edlvpdee edlvpdes
## 1       NA       NA      NA      NA      NA       NA       NA       NA
## 2       NA       NA      NA      NA      NA       NA       NA       NA
## 3       NA       NA      NA      NA      NA       NA       NA       NA
## 4       NA       NA      NA      NA      NA       NA       NA       NA
## 5       NA       NA      NA      NA      NA       NA       NA       NA
## 6       NA       NA      NA      NA      NA       NA       NA       NA
##   edlvpdfi edlvpdfr edupgb1 edupgb2 edagepgb edlvpdgr edlvpdhu edlvpdie
## 1       NA       NA      NA      NA       NA       NA       NA       NA
## 2       NA       NA      NA      NA       NA       NA       NA       NA
## 3       NA       NA      NA      NA       NA       NA       NA       NA
## 4       NA       NA      NA      NA       NA       NA       NA       NA
## 5       NA       NA      NA      NA       NA       NA       NA       NA
## 6       NA       NA      NA      NA       NA       NA       NA       NA
##   edupil1 edupil2 edlvpdlt edlvpdnl edlvpdno edlvpdpl eduyrppl eduppl2
## 1      NA      NA       NA       NA       NA       NA       NA      NA
## 2      NA      NA       NA       NA       NA       NA       NA      NA
## 3      NA      NA       NA       NA       NA       NA       NA      NA
## 4      NA      NA       NA       NA       NA       NA       NA      NA
## 5      NA      NA       10       NA       NA       NA       NA      NA
## 6      NA      NA       13       NA       NA       NA       NA      NA
##   edlvpdpt edlvpdru edlvpdse edlvpdsi edlvpdsk edlvpdua pdwrkp edctnp
## 1       NA       NA       NA       NA       NA       NA      0      0
## 2       NA       NA       NA       NA       NA       NA      0      0
## 3       NA       NA       NA       NA       NA       NA      0      0
## 4       NA       NA       NA       NA       NA       NA      0      0
## 5       NA       NA       NA       NA       NA       NA      1      0
## 6       NA       NA       NA       NA       NA       NA      1      0
##   uemplap uemplip dsbldp rtrdp cmsrvp hswrkp dngothp dngdkp dngnapp
## 1       0       0      0     0      0      0       0      0       1
## 2       0       0      0     0      0      0       0      0       1
## 3       0       0      0     0      0      0       0      0       1
## 4       0       0      0     0      0      0       0      0       1
## 5       0       0      0     0      0      0       0      0       0
## 6       0       0      0     0      0      0       0      0       0
##   dngrefp dngnap mnactp crpdwkp iscocop emprelp emplnop jbspvp njbspvp
## 1       0      0     NA      NA      NA      NA      NA     NA      NA
## 2       0      0     NA      NA      NA      NA      NA     NA      NA
## 3       0      0     NA      NA      NA      NA      NA     NA      NA
## 4       0      0     NA      NA      NA      NA      NA     NA      NA
## 5       0      0     NA      NA    5141       2       0      2      NA
## 6       0      0     NA      NA    1223       1      NA      1       6
##   wkhtotp edulvlfb eiscedf edlvfdbe edlvfdbg edlvfdch edlvfdhr edlvfdcy
## 1      NA        0       1       NA       NA       NA       NA       NA
## 2      NA      313       4       NA       NA       NA       NA       NA
## 3      NA      221       2       NA       NA       NA       NA       NA
## 4      NA      213       2       NA       NA       NA       NA       NA
## 5      40      213       2       NA       NA       NA       NA       NA
## 6      40      113       1       NA       NA       NA       NA       NA
##   edlvfdcz edufde1 edufde2 edufde3 edlvfddk edlvfdee edlvfdes edlvfdfi
## 1       NA      NA      NA      NA       NA       NA       NA       NA
## 2       NA      NA      NA      NA       NA       NA       NA       NA
## 3       NA      NA      NA      NA       NA       NA       NA       NA
## 4       NA      NA      NA      NA       NA       NA       NA       NA
## 5       NA      NA      NA      NA       NA       NA       NA       NA
## 6       NA      NA      NA      NA       NA       NA       NA       NA
##   edlvfdfr edufgb1 edufgb2 edagefgb edlvfdgr edlvfdhu edlvfdie edufil1
## 1       NA      NA      NA       NA       NA       NA       NA      NA
## 2       NA      NA      NA       NA       NA       NA       NA      NA
## 3       NA      NA      NA       NA       NA       NA       NA      NA
## 4       NA      NA      NA       NA       NA       NA       NA      NA
## 5       NA      NA      NA       NA       NA       NA       NA      NA
## 6       NA      NA      NA       NA       NA       NA       NA      NA
##   edufil2 edlvfdlt edlvfdnl edlvfdno edlvfdpl edlvfdpt edlvfdru edlvfdse
## 1      NA        0       NA       NA       NA       NA       NA       NA
## 2      NA        7       NA       NA       NA       NA       NA       NA
## 3      NA        2       NA       NA       NA       NA       NA       NA
## 4      NA        3       NA       NA       NA       NA       NA       NA
## 5      NA        3       NA       NA       NA       NA       NA       NA
## 6      NA        1       NA       NA       NA       NA       NA       NA
##   edlvfdsi edlvfdsk edlvfdua emprf14 emplnof jbspvf occf14b edulvlmb
## 1       NA       NA       NA       1      NA      2       9        0
## 2       NA       NA       NA       1      NA      2       7      520
## 3       NA       NA       NA       1      NA      2       9      221
## 4       NA       NA       NA       1      NA      2       9      113
## 5       NA       NA       NA       1      NA      2       9      213
## 6       NA       NA       NA       1      NA      2       9      113
##   eiscedm edlvmdbe edlvmdbg edlvmdch edlvmdhr edlvmdcy edlvmdcz edumde1
## 1       1       NA       NA       NA       NA       NA       NA      NA
## 2       5       NA       NA       NA       NA       NA       NA      NA
## 3       2       NA       NA       NA       NA       NA       NA      NA
## 4       1       NA       NA       NA       NA       NA       NA      NA
## 5       2       NA       NA       NA       NA       NA       NA      NA
## 6       1       NA       NA       NA       NA       NA       NA      NA
##   edumde2 edumde3 edlvmddk edlvmdee edlvmdes edlvmdfi edlvmdfr edumgb1
## 1      NA      NA       NA       NA       NA       NA       NA      NA
## 2      NA      NA       NA       NA       NA       NA       NA      NA
## 3      NA      NA       NA       NA       NA       NA       NA      NA
## 4      NA      NA       NA       NA       NA       NA       NA      NA
## 5      NA      NA       NA       NA       NA       NA       NA      NA
## 6      NA      NA       NA       NA       NA       NA       NA      NA
##   edumgb2 edagemgb edlvmdgr edlvmdhu edlvmdie edumil1 edumil2 edlvmdlt
## 1      NA       NA       NA       NA       NA      NA      NA        0
## 2      NA       NA       NA       NA       NA      NA      NA       11
## 3      NA       NA       NA       NA       NA      NA      NA        2
## 4      NA       NA       NA       NA       NA      NA      NA        1
## 5      NA       NA       NA       NA       NA      NA      NA        3
## 6      NA       NA       NA       NA       NA      NA      NA        1
##   edlvmdnl edlvmdno edlvmdpl edlvmdpt edlvmdru edlvmdse edlvmdsi edlvmdsk
## 1       NA       NA       NA       NA       NA       NA       NA       NA
## 2       NA       NA       NA       NA       NA       NA       NA       NA
## 3       NA       NA       NA       NA       NA       NA       NA       NA
## 4       NA       NA       NA       NA       NA       NA       NA       NA
## 5       NA       NA       NA       NA       NA       NA       NA       NA
## 6       NA       NA       NA       NA       NA       NA       NA       NA
##   edlvmdua emprm14 emplnom jbspvm occm14b atncrse edul12m useknwl edupdem
## 1       NA       3      NA     NA      NA       2      NA      NA      NA
## 2       NA       1      NA      2       6       1      NA       1       1
## 3       NA       1      NA      2       8       2      NA      NA      NA
## 4       NA       1      NA      2       8       2      NA      NA      NA
## 5       NA       3      NA     NA      NA       2      NA      NA      NA
## 6       NA       1      NA      2       9       1      10       2       1
##   gdsprt clmrlx actvgrs wmcpwrk mnrgtjb gvprppv fltlnla mlohinc dsdclve
## 1      4      5       5       3       5       1       3       6       3
## 2      1      1       2       2       2       1       2       0       0
## 3      5      5       5       1       2       2       2       3       6
## 4      4      4       3       2       2       2       3       3       2
## 5      3      4       4       2       4       2       1       2       2
## 6      3      3       3       2       4       2       2       3       4
##   cuthheq yrspdwka icmnact jbtsktm yrskdwk wrkengt wkovrtm wrkwe yrcremp
## 1       6       35       3      NA      NA      NA      NA    NA      NA
## 2       0        0       3      NA      NA      NA      NA    NA      NA
## 3       6       30       1       4      30       7       1     1    1980
## 4       3       27       1       4      10       1       2     2    2000
## 5       2       32       1       4       3       4       2     2    2008
## 6       3       25       1       2      25       1       2     1    1992
##   emplrjb mnrsefw scrsefw jbcoedu jbedyrs jblrn vrtywrk jbrqlrn wgdpeft
## 1      NA      NA      NA      NA      NA    NA      NA      NA      NA
## 2      NA      NA      NA      NA      NA    NA      NA      NA      NA
## 3      NA       2       6       2      NA     1       2       1       1
## 4       4       2       1      NA      NA     4       3       3       2
## 5      NA       2       1      NA      NA    NA       2       2       2
## 6       4       2       1       1       5     5       3       3       2
##   hlpcowk hlthrwk dcsfwrk jbscr lsintjb wrkhrd nevdnjb oprtad bsmw ppwwkp
## 1      NA      NA      NA    NA      NA     NA      NA     NA   NA     NA
## 2      NA      NA      NA    NA      NA     NA      NA     NA   NA     NA
## 3       2       2       2     2       1      2       2      4    2      5
## 4       3       2       1     2       2      2       4      3    1      2
## 5       3       3       2     3       2      2       4      4    1      1
## 6       3       2       1     2       2      2       4      4    2      6
##   bseftwk smblvjb rpljbde wpmwkcp indisdc truinwk pdaprpa wrywprb trdawrk
## 1      NA      NA      NA      NA      NA      NA      NA      NA      NA
## 2      NA      NA      NA      NA      NA      NA      NA      NA      NA
## 3       5       3      10       2      NA      55       4       3       3
## 4       9       5       5       2      NA      55       3       2       3
## 5       6      NA       3       2      NA      NA       2       2       3
## 6       7       2       7       2      NA       1       4       3       3
##   jbprtfp icnopfma pfmfdjba frsptjb dfcnswka stfmjob stfjbot wkjbndm
## 1      NA       NA       NA      NA       NA      NA      NA      NA
## 2      NA       NA       NA      NA       NA      NA      NA      NA
## 3       2        2       NA      NA       NA      10      10       4
## 4       2        2       NA      NA       NA       7       7       3
## 5       2        2        2       2        2       7       7       3
## 6       2        2        3       2        2       7       7       4
##   grspaya payprda linwk3y rdpay3y wkshr3y lscjb3y orgfd3y npemp3y icb1940
## 1      NA      NA      NA      NA      NA      NA      NA      NA       2
## 2      NA      NA      NA      NA      NA      NA      NA      NA       1
## 3     800       6       2       2       2       2      NA      NA       1
## 4    1400       6       2       2       2       2       3       3       1
## 5      NA      NA       2       2       2       2      NA       3      NA
## 6    1400       6       2       2       1       1       4       2       1
##   ipjbini ipjbscr ipjbhin ipjbwfm ipjbtro pdjbndm uemp3y wkhsch icptn
## 1      NA      NA      NA      NA      NA      NA     NA     NA     2
## 2       5       5       5       5       5       4      9     50     2
## 3       2       5       5       5       4       2      0     40     2
## 4       4       5       5       4       3      NA      0     40     2
## 5       4       4       4       4       3      NA      0     40     1
## 6       4       5       4       3       4       4      0     40     1
##   pwkhsch puemp3y hwwkhs phwwkhs dsgrmnya icptnwka wkengtp wkovtmp ptnwkwe
## 1      NA      NA     NA      NA       NA       NA      NA      NA      NA
## 2      NA      NA     NA      NA       NA       NA      NA      NA      NA
## 3      NA      NA     NA      NA       NA       NA      NA      NA      NA
## 4      NA      NA     NA      NA       NA       NA      NA      NA      NA
## 5      40       0      4      25        1        1       1       3       4
## 6      40       0     18       6        1        1       2       2       2
##   icmnart rtryr wntrtr icb1964 agertr plnchld ipcrtiv imprich ipeqopt
## 1       2    NA     NA       1     NA      NA       5       6       1
## 2       2    NA     NA       2     NA       1       2       3       2
## 3       2    NA     NA       1     55      NA       2       2       2
## 4       2    NA     NA       1     60      NA       4       5       3
## 5       2    NA     NA      NA     NA      NA       4       4       5
## 6       2    NA     NA       1     60      NA       4       4       4
##   ipshabt impsafe impdiff ipfrule ipudrst ipmodst ipgdtim impfree iphlppl
## 1       5       1       2       5       2       1       1       1       3
## 2       2       5       5       1       2       2       2       2       2
## 3       2       2       3       1       2       2       2       1       1
## 4       6       2       6       3       2       2       2       3       3
## 5       3       2       3       2       5      NA       2       2       4
## 6       4       3       5       4       4       3       5       4       4
##   ipsuces ipstrgv ipadvnt ipbhprp iprspot iplylfr impenv imptrad impfun
## 1       4       1       6       1       1       3      1       5      5
## 2       3       5       4       2       3       3      2       4      3
## 3       2       1       2       2       2       2      2       2      2
## 4       2       1       5       4       5       2      2       4      2
## 5       4       3       4       3       3       3      1       2      2
## 6       4       4       5       4       4       4      4       4      5
##   region regunit intewde inwdds inwmms inwyys inwshh inwsmm inwdde inwmme
## 1  LT002       3      NA      4      6   2011     20     21      4      6
## 2  LT008       3      NA     31      5   2011     19     51     31      5
## 3  LT00A       3      NA      3      6   2011     10     20      3      6
## 4  LT003       3      NA      2      6   2011     17     10      2      6
## 5  LT003       3      NA     29      6   2011     17     10     29      6
## 6  LT003       3      NA     27      6   2011     18      5     27      6
##   inwyye inwehh inwemm inwtm spltadmd supqad1 supqad2 supqdd supqmm supqyr
## 1   2011     21      7    46        4       3      NA      4      6   2011
## 2   2011     20     34    43        6       1      NA     31      5   2011
## 3   2011     11     30    70        5       1      NA      3      6   2011
## 4   2011     18     15    65        4       1      NA      2      6   2011
## 5   2011     18     10    60        5       1      NA     29      6   2011
## 6   2011     19      5    60        6       1      NA     27      6   2011
##   dweight pspwght pweight psu samppoin                       stratify
## 1 0.06901 0.08596  0.1687  39       NA 39                            
## 2 0.38445 0.68990  0.1687 135       NA 135                           
## 3 0.27804 0.27598  0.1687 158       NA 158                           
## 4 1.30388 2.97421  0.1687  55       NA 55                            
## 5 2.60776 3.24933  0.1687  55       NA 55                            
## 6 1.15411 1.10065  0.1687  55       NA 55                            
##        prob
## 1 0.0183859
## 2 0.0033002
## 3 0.0045632
## 4 0.0009731
## 5 0.0004865
## 6 0.0009731
# note that the data frame contains far too many variables to be viewed conveniently


# # # # # # # optional data.frame reduction # # # # # # #

# create a character vector that will be used to
# limit the file to only the variables needed
KeepVars <-
    c( 
        # average hours of television viewed
        "tvtot" , 
        
        # child living at home
        "chldhm" ,
    
        # gender
        "gndr" ,
        
        # complex sample survey design variables #
        # ( you need these, don't remove them )  #
        
        # clusters
        "psu" ,
        
        # strata
        "stratify" ,
        
        # probability of sampling
        "prob"
        )

# limit the r data frame (ess5.lt) containing all variables
# to a severely-restricted r data frame containing only the seven variables
# specified in character vector 'KeepVars'
x <- ess5.lt[ , KeepVars ]

# remove the object `ess5.be.m` from memory
rm( list=c("ess5.lt", "KeepVars", "ess5.lt.ms", "ess5.lt.sddf") )

# garbage collection: clear up RAM
gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 294791 15.8     597831 32.0   597831 32.0
## Vcells 540146  4.2    3498104 26.7  4088220 31.2
# # # # # # # end of optional reduction # # # # # # #


# but if you didn't "slim down" your data file,
# you'll need to run this next line of code:
# x <- ess5.lt


#################################################
# survey design for taylor-series linearization #
#################################################

# create a survey design object (ess5.lt.design) with ESS design information.
# note: post-stratification adjustments are not included
ess5.lt.design <- 
    svydesign(
        ids = ~psu ,
        strata = ~stratify ,
        probs = ~prob ,
        data = x
    )
    
# notice the 'ess5.lt.design' object used in all subsequent analysis commands


#####################
# analysis examples #
#####################

# count the total (unweighted) number of records in ESS #

# the nrow function which works on both data frame objects..
class( x )
## [1] "data.frame"
# ..and survey design objects
class( ess5.lt.design )
## [1] "survey.design2" "survey.design"
# number of records in the data.frame object `x`
nrow( x )
## [1] 1677
# same number.  schnazzy, huh?
nrow( ess5.lt.design )
## [1] 1677
# count the weighted number of individuals in ESS #

# add a new variable 'one' that simply has the number 1 for each record #

ess5.lt.design <-
    update( 
        one = 1 ,
        ess5.lt.design
    )
    
# from pdf page 8 of the ESS round five report..
# http://www.europeansocialsurvey.org/docs/round5/survey/ESS5_data_documentation_report_e03_1.pdf
# ..the sample frame is all persons aged 15 and over resident within private households, regardless of their
# nationality, citizenship, language or legal status currently in lithuania.

# and, for a more detailed description of how lithuania's survey was collected, check out pdf page 19
# http://www.europeansocialsurvey.org/docs/round5/survey/ESS5_data_documentation_report_e03_1.pdf#page=121

# here's the sum of the weights..
svytotal( 
    ~one , 
    ess5.lt.design 
)
##       total     SE
## one 1473912 191133
# ..which is completely meaningless on its own.

# count the total (unweighted) number of records in lithuania's data #
# broken out by child living at home #

svyby(
    ~one ,
    ~chldhm ,
    ess5.lt.design ,
    unwtd.count
)
##   chldhm counts se
## 1      1    572  0
## 2      2   1105  0
# calculate the mean of a numeric variable #

# average hours of television viewed - among lithuanian residents aged 15+
svymean( 
    ~tvtot , 
    design = ess5.lt.design ,
    na.rm = TRUE
)
##       mean   SE
## tvtot  4.1 0.14
mean(x$tvtot,na.rm=TRUE) # compare to unweighted mean
## [1] 4.279
sqrt(var(x$tvtot,use="complete.obs")/length(complete.cases(x$tvtot))) # compare to unweighted SE
## [1] 0.05166
# by child living at home
svyby( 
    ~tvtot , 
    ~chldhm ,
    design = ess5.lt.design ,
    svymean ,
    na.rm = TRUE
)
##   chldhm tvtot     se
## 1      1 4.151 0.1671
## 2      2 4.059 0.1789
# calculate the distribution of a categorical variable #

# gndr should be treated as a factor (categorical) variable
# instead of a numeric (linear) variable
# this update statement converts it (1=male, 2=female).
# the svyby command below will not run without this
ess5.lt.design <-
    update( 
        gndr = factor( gndr ) ,
        ess5.lt.design
    )

# gender distribution among lithuanian residents aged 15+
svymean( 
    ~gndr , 
    design = ess5.lt.design ,
    na.rm = TRUE
)
##        mean   SE
## gndr1 0.337 0.02
## gndr2 0.663 0.02
# by child living at home
svyby( 
    ~gndr , 
    ~chldhm ,
    design = ess5.lt.design ,
    svymean , 
    na.rm = TRUE
)
##   chldhm  gndr1  gndr2 se.gndr1 se.gndr2
## 1      1 0.2793 0.7207  0.02838  0.02838
## 2      2 0.3832 0.6168  0.02824  0.02824
# calculate the median and other percentiles #

# note that a taylor-series survey design
# does not allow calculation of standard errors

# minimum, 25th, 50th, 75th, maximum 
# hours of television viewed in lithuania
svyquantile( 
    ~tvtot , 
    design = ess5.lt.design ,
    c( 0 , .25 , .5 , .75 , 1 ) ,
    na.rm = TRUE
)
##       0 0.25 0.5 0.75 1
## tvtot 0    2   4    6 7
# by child living at home
svyby( 
    ~tvtot , 
    ~chldhm ,
    design = ess5.lt.design ,
    svyquantile ,
    c( 0 , .25 , .5 , .75 , 1 ) ,
    keep.var = F ,
    na.rm = TRUE
)
##   chldhm statistic1 statistic2 statistic3 statistic4 statistic5
## 1      1          0          2          4          6          7
## 2      2          0          2          4          6          7
######################
# subsetting example #
######################

# restrict the ess5.lt.design object to
# females only
ess5.lt.design.female <-
    subset(
        ess5.lt.design ,
        gndr == 2
    )

# now any of the above commands can be re-run
# using the ess5.lt.design.female object
# instead of the ess5.lt.design object
# in order to analyze females only
    
# calculate the mean of a numeric variable #

# average hours of television viewed - among lithuanian residents aged 15+, restricted to females
svymean( 
    ~tvtot , 
    design = ess5.lt.design.female ,
    na.rm = TRUE
)
##       mean   SE
## tvtot 4.12 0.17
###################
# export examples #
###################

# calculate the distribution of a categorical variable #
# by child living at home and
# store the results into a new object

gndr.by.chldhm <-
    svyby( 
        ~gndr , 
        ~chldhm ,
        design = ess5.lt.design ,
        svymean ,
        na.rm = TRUE
    )

# print the results to the screen 
gndr.by.chldhm
##   chldhm  gndr1  gndr2 se.gndr1 se.gndr2
## 1      1 0.2793 0.7207  0.02838  0.02838
## 2      2 0.3832 0.6168  0.02824  0.02824
# now you have the results saved into a new object of type "svyby"
class( gndr.by.chldhm )
## [1] "svyby"      "data.frame"
# print only the statistics (coefficients) to the screen 
coef( gndr.by.chldhm )
## 1:gndr1 2:gndr1 1:gndr2 2:gndr2 
##  0.2793  0.3832  0.7207  0.6168
# print only the standard errors to the screen 
SE( gndr.by.chldhm )
##   se.gndr1 se.gndr2
## 1  0.02838  0.02838
## 2  0.02824  0.02824
# this object can be coerced (converted) to a data frame.. 
gndr.by.chldhm <- data.frame( gndr.by.chldhm )

# ..and then immediately exported as a comma-separated value file 
# into your results directory
setwd("/.../asdfree.com/European Social Survey/results/")
write.csv( gndr.by.chldhm , "gndr by chldhm.csv" )

# ..or trimmed to only contain the values you need.

# here's the percent of lithuanian residents aged 15+ who are female
# broken down by having a child at home, with accompanying standard errors
female.rate.by.chldhm <-
    gndr.by.chldhm[ , c( "chldhm" , "gndr2" , "se.gndr2" ) ]
# that's all rows, and the three specified columns

# print the new results to the screen
female.rate.by.chldhm
##   chldhm  gndr2 se.gndr2
## 1      1 0.7207  0.02838
## 2      2 0.6168  0.02824
# this can also be exported as a comma-separated value file 
# into your current working directory 
write.csv( female.rate.by.chldhm , "female rate by chldhm.csv" )

# ..or directly made into a bar plot
barplot(
    100*female.rate.by.chldhm[ , 2 ] ,                                                  # the second column contains the main statistics
    main = "% of 15+ Year old Lithuanians who are Female, by Child Living at Home" ,    # title the barplot
  sub="Design wighted results",                                         # subtitle the barplot
    names.arg = c( "Child at Home" , "No Child in Household" ) ,                    # title the bars
    ylim = c( 0 , 100 ),                                                            # set the lower and upper bound of the y axis
  col = "darkgreen"                                   # set colors for the bars
)

plot of chunk unnamed-chunk-1

# for more details on how to work with data in r
# check out my two minute tutorial video site
# http://www.twotorials.com/

# dear everyone: please contribute your script.
# have you written syntax that precisely matches an official publication?
message( "if others might benefit, send your code to ajdamico@gmail.com" )
## if others might benefit, send your code to ajdamico@gmail.com
# http://asdfree.com needs more user contributions

# let's play the which one of these things doesn't belong game:
# "only you can prevent forest fires" -smokey bear
# "take a bite out of crime" -mcgruff the crime pooch
# "plz gimme your statistical programming" -anthony damico