rnorouzian
rnorouzian

Reputation: 7517

Extract part of a regression type output

Below, I was wondering how I could extract the last part of the output of result titled Likelihood Ratio Test?

library(weightr)

y <- dat.bangertdrowns2004$yi
v <- dat.bangertdrowns2004$vi

(result <- weightfunct(y, v))
# .
# .
# .
Likelihood Ratio Test:                     ## This part
X^2(df = 1) = 1.163544, p-val = 0.28073    ## This part

Upvotes: 1

Views: 108

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269371

The indicated output is computed in print.weightfunct so we can copy the print method and then add a trace to it which captures and outputs the required information into the one row data frame .W :

print.weightfunct <- weightr:::print.weightfunct
trace(print.weightfunct, exit = quote(.W <<- data.frame(df, lrchisq, pvalue)))
(result <- weightfunct(y, v))
## ...snip ## 
.W
##   df  lrchisq    pvalue
## 1  1 1.163544 0.2807317

Upvotes: 2

user20650
user20650

Reputation: 25844

Another option is to look at the function that produces the output. In this case it is weightr:::print.weightfunct and the relevant lines are

<snip>
        cat("\n")
        cat("Likelihood Ratio Test:")
        cat("\n")
        df <- length(x[[2]]$par) - length(x[[1]]$par)
        lrchisq <- 2 * (abs(x[[1]]$value - x[[2]]$value))
        pvalue <- 1 - pchisq(lrchisq, df)
        cat("X^2(df = ", df, ") = ", lrchisq, ", p-val = ", format.pval(pvalue), 
            sep = "")
</snip>

So you can grab the values with

x = result
df <- length(x[[2]]$par) - length(x[[1]]$par)
lrchisq <- 2 * (abs(x[[1]]$value - x[[2]]$value))
pvalue <- 1 - pchisq(lrchisq, df)

> lrchisq
#[1] 1.163544
> pvalue
#[1] 0.2807317

Upvotes: 2

akrun
akrun

Reputation: 886938

An option is

out <- capture.output(result)
out[cumsum(grepl("Likelihood", out)) > 0]
#[1] "Likelihood Ratio Test:"                 
#[2] "X^2(df = 1) = 1.163544, p-val = 0.28073"

Upvotes: 1

Related Questions