user20536224
user20536224

Reputation:

how to get df from `mgcv::logLik.gam` output

In R, package mgcv has logLik.gam that gives a generalized additive model's loglikelihood value as well as df. However, I could not take the df value only.

For example,

dt= gamSim(1,50) 
x= dt$x0
y=dt$x1
model= gam(y~s(x)) 
logLik.gam(model) 
[1] 'log Lik.' -1.560092 (df=3.612741)

From this I can only take -1.560092 . But to use the value of df (that is 3.612741), is there any way to take this from the output? The output here is numeric which is actually a sentence. So is there any way to cutting this output line and taking only the last value?

Upvotes: 0

Views: 272

Answers (2)

R student
R student

Reputation: 129

Extracting log likelihood from a GAMM using mgcv. For the updated method use: attributes(logLik(model$gam))$df

mgcv package (version 1.8-42) - https://www.rdocumentation.org/packages/mgcv/versions/1.8-42 Last published: March 2nd, 2023

other useful values to extract from GAMM can be AIC, BIC and adjusted R-squared.

Note: for GAMMs use lme for the AIC and BIC, not model$gam.

aic <- AIC(model$lme)
bic <- BIC(model$lme)
adj_rsq <- summary(model$gam)$r.sq

Upvotes: 0

IRTFM
IRTFM

Reputation: 263481

Yes. If you first assign the result to an output variable name you can then work with it. There is a print method for objects of that class:

res <- logLik.gam(model)  
print( res)
'log Lik.' -4.613525 (df=4.322011)

attributes( res)
 #-----
$df
[1] 4.322011

$class
[1] "logLik"

 attributes( res)$df
#[1] 4.322011

Upvotes: 4

Related Questions