if (FALSE) {
# ################################################
# Real-world example: not run because takes long.
# Build panel with income, wage, age and education
# optionally: add wealth supplements!
# ################################################
# The package is installed with a list of variables
# Alternatively, search for names with \code{\link{getNamesPSID}}
# This is the body of function build.psid()
# (so why not call build.psid() and see what happens!)
r = system.file(package="psidR")
if (small){
f = fread(file.path(r,"psid-lists","famvars-small.txt"))
i = fread(file.path(r,"psid-lists","indvars-small.txt"))
} else {
f = fread(file.path(r,"psid-lists","famvars.txt"))
i = fread(file.path(r,"psid-lists","indvars.txt"))
}
setkey(i,"name")
setkey(f,"name")
i = dcast(i[,list(year,name,variable)],year~name)
f = dcast(f[,list(year,name,variable)],year~name)
d = build.panel(datadir="~/datasets/psid/",fam.vars=f,
ind.vars=i,
heads.only =TRUE,sample="SRC",
design="all")
save(d,file="~/psid.RData")
}
# ######################################
# reproducible example on artifical data.
# run this with example(build.panel).
# ######################################
## make reproducible family data sets for 2 years
## variables are: family income (Money) and age
## Data acquisition step:
## run build.panel with sascii=TRUE
# testPSID creates artifical PSID data
td <- testPSID(N=12,N.attr=0)
fam1985 <- data.table::copy(td$famvars1985)
fam1986 <- data.table::copy(td$famvars1986)
IND2019ER <- data.table::copy(td$IND2019ER)
# create a temporary datadir
my.dir <- tempdir()
#save those in the datadir
# notice different R formats admissible
save(fam1985,file=paste0(my.dir,"/FAM1985ER.rda"))
save(fam1986,file=paste0(my.dir,"/FAM1986ER.RData"))
save(IND2019ER,file=paste0(my.dir,"/IND2019ER.RData"))
## end Data acquisition step.
# now define which famvars
famvars <- data.frame(year=c(1985,1986),
money=c("Money85","Money86"),
age=c("age85","age86"))
# create ind.vars
indvars <- data.frame(year=c(1985,1986),ind.weight=c("ER30497","ER30534"))
# call the builder
# data will contain column "relation.head" holding the relationship code.
d <- build.panel(datadir=my.dir,fam.vars=famvars,
ind.vars=indvars,
heads.only=FALSE)
# see what happens if we drop non-heads
# only the ones who are heads in BOTH years
# are present (since design='balanced' by default)
d <- build.panel(datadir=my.dir,fam.vars=famvars,
ind.vars=indvars,
heads.only=TRUE)
print(d[order(pid)],nrow=Inf)
# change sample design to "all":
# we'll keep individuals if they are head in one year,
# and drop in the other
d <- build.panel(datadir=my.dir,fam.vars=famvars,
ind.vars=indvars,heads.only=TRUE,
design="all")
print(d[order(pid)],nrow=Inf)
file.remove(paste0(my.dir,"/FAM1985ER.rda"),
paste0(my.dir,"/FAM1986ER.RData"),
paste0(my.dir,"/IND2019ER.RData"))
# END psidR example
# #####################################################################
# Please go to https://github.com/floswald/psidR for more example usage
# #####################################################################
Run the code above in your browser using DataLab