take the following model as an example
library('lme4')
foo <- lmer(cty ~ hwy + (1|model), data=mpg, REML=F)
Now we can retreive the LogLikelihood of the model
sum.foo <- summary(foo)
LL <- sum.foo[["logLik"]]
LL
'log Lik.' -343 (df=4)
typeof(LL)
[1] "double"
Question: how can all this information be stored in a double object? There seems to be at least two strings in there, i.e. "log Lik." and "(df=4)". Is there a way to retreive the value of df from this object?
LL
is really just a number (with attributes). The line you see when you print LL
is created by the print.logLik
function as you can see below:
getAnywhere(print.logLik)
A single object matching ‘print.logLik’ was found
It was found in the following places
registered S3 method for print from namespace stats
namespace:stats
with value
function (x, digits = getOption("digits"), ...)
{
cat("'log Lik.' ", paste(format(c(x), digits = digits), collapse = ", "),
" (df=", format(attr(x, "df")), ")\n", sep = "")
invisible(x)
}
This is what gets called when you run LL
but LL
is really only one number (numeric vector of length 1). The cat
function is what prints the 'log Lik.' -343 (df=4)
that you see on your console.
In order to get the df value you could do (as it can also be seen from the function above):
format(attr(LL[['logLik']], "df"))
See an example (from lmer
's documentation):
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
a <- summary(fm1)# (with its own print method)
> a[['logLik']]
'log Lik.' -871.8141 (df=6)
> format(attr(a[['logLik']], "df"))
[1] "6"
And as per @BenBolker mentions in the comment format
only converts it into character.
> attr(a[['logLik']], "df")
[1] 6
Is probably better.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments