1) Never attach a data set because you will get the error that you got
below. Try your recodes without attaching.
2) Only load the R libraries that you need for the session. Based on a
quick scan of your code, you don't need any of the packages that you've
loaded. Note that function names in packages conflict, so when you load
more than you need, you introduce potential instability in the R session.
3) Rather than use recode() as you have above, you should do
var[is.na(var)] <- 0
Yours,
Olivia
On Mon, Nov 7, 2011 at 12:26 PM, Laura Lazarus Frankel
<llfrankel(a)gmail.com>wrote;wrote:
Hi All,
Fledgling graduate student here. I'm trying to run zelig for a logit
model that will run in the R base package, but no matter what I seem to do
to the recoded variables (attach, detach, reattach, put them back into the
dataset, create a new dataset, etc.) I keep getting the following error
message:
Error in `[.data.frame`(d, , all.vars(as.expression(formula))) :
undefined columns selected
The most concise form of the code is below.
Thanks in advance!
Laura
library(foreign)
library(car)
library(survival)
library(lmeSplines)
library(MASS)
library(nnet)
library(nlme)
library(reshape)
library(plyr)
library(grid)
library(proto)
library(ggplot2)
library(lattice)
gss<-read.dta('/Users/Laura/Dropbox/projects/PanelAttrition/GSS
Data/GSS.dta')
names(gss)
summary(gss$panstat_2)
summary(gss$panstat_3)
gssattrition<-c()
gssattrition[gss$panstat_3=='selected, eligible, but not reinterviewed']<-
1
gssattrition[gss$panstat_2=='selected, eligible, but not reinterviewed']<-
1
gssattrition[gss$panstat_3=='selected, eligible, and reinterviewed']<-0
summary(gssattrition)
gssattrition<-c()
gssattrition[gss$panstat_3=='selected, eligible, but not reinterviewed']<-
1
gssattrition[gss$panstat_3=='attrited']<-1
gssattrition[gss$panstat_3=='selected, eligible, and reinterviewed']<-0
gssattrition[gss$panstat_2=='selected, but not eligible and not
reinterviewd']<-NA
gssattrition[gss$panstat_2=='selected, but not eligible and not
reinterviewd because r was in institution']<-NA
gssattrition[gss$panstat_2=='selected, but not eligible and not
reinterviewd because r was in institution']<-NA
gssattrition[gss$panstat_2=='selected, but not eligible and not
reinterviewd because r was deceased']<-NA
summary(gssattrition)
gssfulltime<-c()
gssfulltime[gss$wrkstat_1=='working fulltime']<-1
gssfulltime[gss$wrkstat_1=='working parttime']<-1
gssfulltime<-recode(gssfulltime, 'NA=0')
gssforeign<-c()
gssforeign[gss$born_1=='yes']<-0
gssforeign[gss$born_1=='no']<-1
gsshome<-c()
summary(gss$dwelown_1)
gsshome[gss$dwelown_1=='own or is buying']<-1
gsshome[gss$dwelown_1=='pays rent']<-0
gsshome[gss$dwelown_1=='other']<-0
summary(gsshome)
gssrace<-c()
gssrace[gss$race_1=='2']<-1
gssrace[gss$race_1=='1']<-0
gssrace[gss$race_1=='3']<-1
gssincome<-c()
gssincome[gss$income06_1=='under $1 000']<-1
gssincome[gss$income06_1=='$1 000 to 2 999']<-1
gssincome[gss$income06_1=='$3 000 to 3 999']<-1
gssincome[gss$income06_1=='$4 000 to 4 999']<-1
gssincome[gss$income06_1=='$5 000 to 5 999']<-2
gssincome[gss$income06_1=='$6 000 to 6 999']<-2
gssincome[gss$income06_1=='$7 000 to 7 999']<-2
gssincome[gss$income06_1=='$8 000 to 9 999']<-3
gssincome[gss$income06_1=='$10000 to 12499']<-4
gssincome[gss$income06_1=='$12500 to 14999']<-5
gssincome[gss$income06_1=='$15000 to 17499']<-6
gssincome[gss$income06_1=='$17500 to 19999']<-6
gssincome[gss$income06_1=='$20000 to 22499']<-7
gssincome[gss$income06_1=='$22500 to 24999']<-7
gssincome[gss$income06_1=='$25000 to 29999']<-8
gssincome[gss$income06_1=='$30000 to 34999']<-9
gssincome[gss$income06_1=='$35000 to 39999']<-10
gssincome[gss$income06_1=='$50000 to 59999']<-12
gssincome[gss$income06_1=='$40000 to 49999']<-11
gssincome[gss$income06_1=='$60000 to 74999']<-13
gssincome[gss$income06_1=='$75000 to $89999']<-14
gssincome[gss$income06_1=='$90000 to $109999']<-15
gssincome[gss$income06_1=='$110000 to $129999']<-16
gssincome[gss$income06_1=='$130000 to $149999']<-17
gssincome[gss$income06_1=='$150000 or over']<-18
summary(gssincome)
summary(gss$degree_1)
gsseducation<-c()
gsseducation[gss$degree_1=='lt high school']<-1
gsseducation[gss$degree_1=='high school']<-2
gsseducation[gss$degree_1=='junior college']<-3
gsseducation[gss$degree_1=='bachelor']<-4
gsseducation[gss$degree_1=='graduate']<-5
gssage<-gss$age_1
gssalone<-c()
gssalone[gss$hompop_1=='1']<-1
gssalone<-recode(gssalone, 'NA=0')
gssexperience<-gss$intyrs_1
gsslength<-gss$lngthinv_1
gssfee<-gss$feelevel_1
gssfee[gss$feeused_1=='no']<-0
gssface<-c()
gssface[gss$mode_1=='in-person']<-1
gssface[gss$mode_1=='over the phone']<-0
gssface[gss$mode_1=='in-person/phone']<-0
gssintwoman<-c()
summary(gss$intsex_1)
gssintwoman[gss$intsex_1=='female']<-1
gssintwoman<-recode(gssintwoman, 'NA=0')
gssintwhite<-c()
gssintwhite[gss$intrace1_1=='white']<-1
gssintwhite<-recode(gssintwhite, 'NA=0')
gsshispanic<-c()
gsshispanic[gss$hispanic_1=='not hispanic']<-0
gsshispanic<-recode(gsshispanic, 'NA=1')
gssracesame<-c()
gssracesame[gss$intrace1_1=='white' &
gss$racecen1_1=='white']<-1
gssracesame[gss$intrace1_1=='black or african american' &
gss$racecen1_1=='black
or african american']<-1
gssracesame[gss$intrace1_1=='asian indian' & gss$racecen1_1=='asian
indian']<-1
gssracesame[gss$intrace1_1=='chinese' &
gss$racecen1_1=='chinese']<-1
gssracesame[gss$intrace1_1=='filipino' &
gss$racecen1_1=='filipino']<-1
gssracesame[gss$intrace1_1=='japanese' &
gss$racecen1_1=='japanese']<-1
gssracesame[gss$intrace1_1=='korean' &
gss$racecen1_1=='korean']<-1
gssracesame[gss$intrace1_1=='vietnamese' &
gss$racecen1_1=='vietnamese']<-
1
gssracesame[gss$intrace1_1=='other asian' & gss$racecen1_1=='other
asian'
]<-1
gssracesame[gss$intrace1_1=='native hawaiian' &
gss$racecen1_1=='native
hawaiian']<-1
gssracesame[gss$intrace1_1=='other pacific islander' &
gss$racecen1_1=='other
pacific islander']<-1
gssracesame[gss$intrace1_1=='some other race' & gss$racecen1_1=='some
other race']<-1
gssracesame[gss$intrace1_1=='hispanic' &
gss$racecen1_1=='hispanic']<-1
gssracesame[gsshispanic=='1' & gss$intethn=='hispanic']<-1
gssracesame<-recode(gssracesame, 'NA=0')
gsssexsame<-c()
gsssexsame[gss$sex_1=='1' & gssintwoman=='0']<-1
gsssexsame[gss$sex_1=='2'& gssintwoman=='1']<-1
gsssexsame<-recode(gsssexsame, 'NA=0')
gsssex<-c()
gsssex[gss$sex_1=='1']<-0
gsssex[gss$sex_1=='2']<-1
gssvote04<-c()
gssvote04[gss$vote04_1=='voted']<-1
gssvote04[gss$vote04_1=='did not vote']<-0
summary(gssvote04)
summary(gss$babies_1)
gchildren<-gss$babies_1
summary(gchildren)
gss$gssattriiton1<-gssattrition
gss$gssage1<-gssage
gss$gsssex1<-gsssex
gss$gssrace1<-gssrace
gss$gssfulltime1<-gssfulltime
gss$gsseducation1<-gsseducation
gss$gssattriiton1<-gssincome
gss$gsshome1<-gsshome
gss$gssfulltime1<-gssfulltime
gss$gchildren1<-gchildren
gss$gssforeign1<-gssforeign
gss$gssattriiton1<-gssvote04
gss$gssalone1<-gssalone
gss$gssexperience1<-gssexperience
gss$gssintwoman1<-gssintwoman
gss$gsssexsame1<-gsssexsame
library(Zelig)
gsslogit<- zelig(gssattrition1~gssage1+gsssex1+gssrace1+gssfulltime1+
gsseducation1+gssincome1+gsshome1+gssfulltime1+gchildren1+gssforeign1+
gssvote041+gssalone1+gssexperience1+gssintwoman1+gsssexsame1, model=
"logit", data=gss)
gss.data<-gss[,c(1833, 1855:1867)]
names(gss.data)
gsslogit<- zelig(gssattrition1~gssage1+gsssex1+gssrace1+gssfulltime1+
gsseducation1+gssincome1+gsshome1+gssfulltime1+gchildren1+gssforeign1+
gssvote041+gssalone1+gssexperience1+gssintwoman1+gsssexsame1, model=
"logit", data=gss.data)