[R] Logical Indicator for Warning and Error Messages?

2011-01-02 Thread David Crow
Dear R Community-

 

Is there a logical variable indicating the presence of a warning or error
message after executing a command?  I’m bootstrapping a logit model (1,000
iterations, relevant code posted below), but the model fails to converge on
many iterations.  (I’m guessing that a small sample size and the combination
of sampling with replacement, high item missingness, and list-wise deletion
leads to a sparse data problem for some iterations.)

 

Even for the iterations that fail to converge, though, the program produces
parameter estimates.   I would like to remove these parameter estimates from
N x k matrix containing the boostrapped parameter estimates.  What I was
thinking of is adding a line of code to check if there is a warning or error
message present, recording the iteration numbers where there are
error/warning messages, and replacing the parameters for those iterations
with NA’s.  I’ve checked the help pages for the “warning”, “getOption”, and
other commands, but it’s not immediately apparent to me how to check for the
presence of warning or error messages.  

 

Here’s the relevant portion of the code (I’ve omitted the parts that recover
parameter and fit estimates for each iteration):

 

*

[Code Starts Here]



#Create Objects

N = 1000   #1000 iterations

set.seed(7843)

seed = sample(1000, 1000)



#1000 Iterations

for (i in 1:N){



#Bootstap (Sampling with Replacement)

set.seed(seed[i])

pre <- subset(cainit, time==1)

sampt1 <- sort(sample(nrow(pre), nrow(pre), replace=T))

sampt1 <- pre[sampt1,]





#T1 Model of Vote Intention

try(T1A <- glm(prop1a ~ educ + age + inc + dem + appgov + appleg + budget +
co1a + 

prop1b + prop1d,family=binomial(link="logit"), 

data=sampt1, na.action=na.exclude))



##[NOTE:  the “lrm” command is part of the “Design” package]

try(T1a <- lrm(prop1a ~ educ + age + inc + dem + appgov + appleg + budget +
co1a + 

prop1b + prop1d, data=sampt1))

}



[Code Ends Here]

**







The following set of warning/error messages is repeated about a dozen times
at the end of the loop:



Error in lrm(prop1a ~ educ + age + inc + dem + appgov + appleg + budget +  :


  Unable to fit model using “lrm.fit”

In addition: Warning messages:

1: glm.fit: fitted probabilities numerically 0 or 1 occurred 

2: glm.fit: fitted probabilities numerically 0 or 1 occurred 

3: glm.fit: algorithm did not converge 

4: glm.fit: fitted probabilities numerically 0 or 1 occurred







Any help would be greatly appreciated.  



Happy New Year!

David



======

David Crow

Profesor-Investigador / Assistant Professor

División de Estudios Internacionales

Centro de Investigación y Docencia Económicas


Carretera México-Toluca 3655
Col. Lomas de Santa Fe
01210 México, D.F.
Tel.:   (+011 52 55) 5727-9800, ext. 2152



==




[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Help w/ variable names in loop with lmer

2015-03-29 Thread David Crow
Hi, R users-

I'm estimating random effects models with cross-level interactions; I want
to interact each of a vector of level-1 variables with each of a vector of
level-2 variables.  Here's the code:


#create data frame with level-1 variables
k <- as.data.frame(cbind(robo, asalto, secuestro, asesinato))

#create data frame with level-2 variables
l <- as.data.frame(cbind(IDH_IDH, IDH_ingpc, eco_pb, IM_indice, tasa_robo,
hom_tasa, totdelitos1, totdelitos2, total, pri, pan, prd))

#get cross-level interactions

for (i in 1:length(k)) {
for (j in 1:length(l)) {
print(summary(lmer(hrprotcrim ~ k[,i]*l[,j] + (k[,i] | Municipio
}
}
==

The code works and produces 48 (4 level-1 x 12 level-2) sets of output.
The problem is, the output is illegible because instead of the variable
names, I get the indices:

[output]
==
Linear mixed model fit by REML ['lmerMod']
Formula: hrprotcrim ~ k[, i] * l[, j] + (k[, i] | Municipio)

REML criterion at convergence: 8801.4

Scaled residuals:
Min  1Q  Median  3Q Max
-2.4447 -0.7017 -0.2639  0.6766  3.0835

Random effects:
 GroupsNameVariance Std.Dev. Corr
 Municipio (Intercept) 1.067868 1.0334
   k[, i]  0.005387 0.0734   1.00
 Residual  2.976150 1.7252
Number of obs: 2163, groups:  Municipio, 180

Fixed effects:
   Estimate Std. Error t value
(Intercept)2.710847   0.101715  26.651
k[, i]-0.056720   0.355802  -0.159
l[, j] 0.002701   0.002289   1.180
k[, i]:l[, j]  0.006510   0.006340   1.027

Correlation of Fixed Effects:
(Intr) k[, i] l[, j]
k[, i]  -0.048
l[, j]  -0.514  0.028
k[,i]:l[,j]  0.034 -0.566 -0.072
==

Two questions:

1)  How can I get variable names instead of indices in the above output
2)  How can I estimate this with "mapply" instead of the double loop?

Here's the code for "mapply"

M4 <- mapply(function(k,l){summary(lmer(hrprotcrim ~ k*l + (k |
Municipio)))})

And here's what I get:

list()

I'd be grateful for any pointers.

Best,
David



-- 
Personal Web site:
http://investigadores.cide.edu/crow/

Web site for M�xico, las Am�ricas y el Mundo:
http://mexicoyelmundo.cide.edu/


David Crow, Ph.D.
Profesor-Investigador/Assistant Professor
Director General, *Las Am�ricas y el Mundo*
Divisi�n de Estudios Internacionales
Carretera M�xico-Toluca 3655
Col. Lomas de Santa Fe 01210  M�xico, D.F.
Tel.:  5727-9800, ext. 2152
Fax:  5727-9872


Conmutador: 5727-98-00 Lada sin costo: 01 800 021 2433 (CIDE) |�

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.