Amanda Goldberg
Amanda Goldberg

Reputation: 363

Creating an AIC model selection table with model included

I have been using code as explained here:How to Create AIC Model Selection Table in R in LaTex format?

However, I am running into an error with my most recent dataset and I think it has to do with having a larger number of model terms (I have 11 columns with model terms)

Below is my code:

library(MuMIn)
out.put<-model.sel(m1,  m2, m3, m4, m5, m6, m7, m8, m9, m10,    m11,    m12,    m13,    m14,    m15,    m16,    m17,    m18,    m19,    m20,    m21,    m22,    m23,    m24,    
                m25,    m26,    m27,    m28,    m29,    m30,    m31,    m32,    m33,    m34,    m35,    m36,    m37,    m38,    m39,    m40,    m41,    m42,    m43,    m44,    m45,    
                m46,    m47,    m48,    m49,    m50,    m51,    m52,    m53,    m54,    m55,    m56,    m57,    m58,    m59,    m60,    m61,    m62,    m63,    m64,    m65,    m66,    
                m67,    m68,    m69,    m70,    m71,    m72,    m73,    m74,    m75,    m76,    m77,    m78,    m79,    m80,    m81,    m82,    m83,    m84,    m85,    m86,    m87,    
                m88,    m89,    m90,    m91,    m92,    m93,    m94,    m95,    m96,    m97,    m98,    m99,    m100,   m101,   m102,   m103,   m104,   m105,   m106,   m107,   m108,   
                m109,   m110,   m111,   m112,   mnull)
i <- 1:11 # indices of columns with model terms
response <- "Survival"
res <- as.data.frame(out.put)
v <- names(out.put)[i]
v[v == "(Intercept)"] <- 1
# create formula-like model names:
mnames <- apply(res[, i], 1, function(x)
  deparse(simplify.formula(reformulate(v[!is.na(x)], response = response))))
res <- cbind(model = mnames, res[, -i])

Everything looks ok until I look at 'mnames' where some of the models look like this:

$m19

[1] "Survival ~ Inj * Year + Trt * Year + Trt * Sex + Inj * Trt + " " Inj * Sex + Year * Sex"

and I get this error when I run the final line of code: Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 113

I assume the issue is with the " " in the mnames but I can't figure out why it's doing that. I have tried to shorten the names to see if it is due to the size but that did not seem to work (though maybe I did not shorten everything enough). I also tried to remove the 'simplify.formula' in case it was due to that but still the same problem. Any ideas on how I can fix this?

Thank you

In response to comment by SamR

> sapply(out.put, class)
$`(Intercept)`
[1] "numeric"

$Inject
[1] "factor"

$Trt
[1] "factor"

$Year
[1] "factor"

$`Inject:Year`
[1] "factor"

$`Trt:Year`
[1] "factor"

$Sex
[1] "factor"

$`Sex:Trt`
[1] "factor"

$`Inject:Trt`
[1] "factor"

$`Inject:Sex`
[1] "factor"

$`Sex:Year`
[1] "factor"

$family
[1] "character"

$df
[1] "integer"

$logLik
[1] "numeric"

$AICc
[1] "numeric"

$delta
[1] "numeric"

$weight
[1] "model.weights" "numeric"      

> sapply(out.put, dim)
$`(Intercept)`
NULL

$Inject
NULL

$Trt
NULL

$Year
NULL

$`Inject:Year`
NULL

$`Trt:Year`
NULL

$Sex
NULL

$`Sex:Trt`
NULL

$`Inject:Trt`
NULL

$`Inject:Sex`
NULL

$`Sex:Year`
NULL

$family
NULL

$df
NULL

$logLik
NULL

$AICc
NULL

$delta
NULL

$weight
NULL

Upvotes: 0

Views: 995

Answers (2)

Bhawit balodi
Bhawit balodi

Reputation: 1

import tensorflow as tf 

from scipy.stats import norm

  Function for calculating AIC
  def AIC_value(model,Y_pred,Y_test):

    Y_test = np.asarray(Y_test).astype('float32').reshape((-1,1))
    Y_pred_1 = np.asarray(Y_pred).astype('float32').reshape((-1,1))



    nll = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=Y_test, logits=Y_pred_1))

    --Calculate the number of parameters in the model
    num_params = model.count_params()

    --Calculate the AIC value for the model
    aic = 2 * num_params - 2 * nll

    --Print the AIC value
    print("AIC: ", aic.numpy())

Upvotes: 0

Amanda Goldberg
Amanda Goldberg

Reputation: 363

Just to clarify for those interested in this question, I simply needed add width.cutoff=100L to the mnames line of code.

# create formula-like model names:
mnames <- apply(res[, i], 1, function(x)
  deparse(simplify.formula(reformulate(v[!is.na(x)], response = response)), width.cutoff = 100L))

It now works perfectly

Upvotes: 1

Related Questions