user2484516
user2484516

Reputation: 21

knitr + lyx: an error in practice of exact logistic regression

As you may know, there is a technique, called exact logistic regression. The idre from UCLA provides excellent analysis example. (http://www.ats.ucla.edu/stat/r/dae/exlogit.htm) When I exercise with the example analysis, I face an error for knitr (+ lyx).

In the middle of a list of syntax in the Webpage, the syntax below produces an error. Knitr seems to interpret the chunk well, but lyx produces an error related to pdf output.

<<>>=
## model with female predictor only'
m.female <- elrm(formula = admit/ntrials ~ female, interest = ~female, iter = 22000, dataset = cdat, burnIn = 2000)
@

lyx produces an error message like this:

Running: pdflatex  "logistics.regression.tex" > /dev/null
11:50:28.071: Error while exporting format: pdf2../../../src/support/Systemcall.cpp(270): Systemcall: 'pdflatex  "logistics.regression.tex"' finished with exit code 1

Meanwhile, In the error box of lyx, I see the message below:

\end{verbatim}                    
You need to provide a definition with \DeclareInputText or \DeclareInputMath before using this key

The syntax above in the chunk, however, works well in R terminal without any problem. Should I add an knitr option for the chunk? Then, what is it? Thank you in advance.

Jong-Hwa

require(knitr)
require(elrm)
dat <- read.table(text = " 
female  apcalc    admit       num
 0        0        0         7
 0        0        1         1
 0        1        0         3 
 0        1        1         7
 1        0        0         5
 1        0        1         1
 1        1        0         0
 1        1        1         6",
   header = TRUE)
dat
summary(dat)
dat <- dat[rep(1:nrow(dat), dat$num), -4]
summary(dat)
xtabs(~ female + apcalc, data=dat)
xtabs(~ female + admit, data=dat)
xtabs(~ apcalc + admit, data=dat)
xtabs(~ female + apcalc + admit, data=dat)
x <- xtabs(~admit + interaction(female, apcalc), data = dat) 
x  # view cross tabs
cdat <- data.frame(female = rep(1:0, 2), apcalc = rep(1:0, each = 2), admit = x[1, ], ntrials = colSums(x)) 
cdat  # view collapsed data set
## model with female predictor only
m.female <- elrm(formula = admit/ntrials ~ female, interest = ~female, iter = 22000, dataset = cdat, burnIn = 2000)
####### the last syntax causes an error in lyx (+ knitr)

Upvotes: 1

Views: 279

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30184

The problem came from the progress bar (specifically the backspace \b). Two possible solutions:

  1. Contact the author of elrm and request an argument progress = TRUE/FALSE in elrm() so that you can suppress the progress bar with, e.g., elrm(..., progress = FALSE);
  2. Move the call to elrm() to a separate chunk and hide the output of the progress bar, e.g.,

    <<>>=
    require(elrm)
    dat <- read.table(text = "
    female  apcalc    admit       num
     0        0        0         7
     0        0        1         1
     0        1        0         3 
     0        1        1         7
     1        0        0         5
     1        0        1         1
     1        1        0         0
     1        1        1         6",
       header = TRUE)
    dat
    summary(dat)
    dat <- dat[rep(1:nrow(dat), dat$num), -4]
    summary(dat)
    xtabs(~ female + apcalc, data=dat)
    xtabs(~ female + admit, data=dat)
    xtabs(~ apcalc + admit, data=dat)
    xtabs(~ female + apcalc + admit, data=dat)
    x <- xtabs(~admit + interaction(female, apcalc), data = dat) 
    x  # view cross tabs
    cdat <- data.frame(female = rep(1:0, 2), apcalc = rep(1:0, each = 2), admit = x[1, ], ntrials = colSums(x)) 
    cdat  # view collapsed data set
    ## model with female predictor only
    @
    
    <<results='hide'>>=
    m.female <- elrm(formula = admit/ntrials ~ female, interest = ~female, iter = 22000, dataset = cdat, burnIn = 2000)
    @
    
    <<>>=
    summary(m.female)
    @
    

Upvotes: 2

Related Questions