0

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
Amanda Goldberg
  • 169
  • 1
  • 11
  • 2
    Have you really manually created 113 different models named `m1` to `m112`, and `mnull`?! Unfortunately, your example is not reproducible as you have posted it because we do not have the data, or what the models were based on. However, fundamentally, it looks like different models have a different number of rows and do not like being coerced into a data frame. What do you get when you run `sapply(out.put, class)` and `sapply(out.put, dim)`? – SamR Jan 17 '22 at 17:03
  • SamR. Thank you for your quick response. The data has 4 variables (all factors) and I did make 113 different models. These are all logistic regression models. – Amanda Goldberg Jan 18 '22 at 19:32
  • Out of interest, how do you create 113 different models from 4 variables? Even with interactions and second order terms I don't think I could get that many! In any case, so many variables seems complicated. It will be a lot easier to help if you create a minimal reproducible example - maybe with a subset of the rows of the data and and just model `m19` as there does seem be something off about the return value: See here for more: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – SamR Jan 19 '22 at 09:31
  • 1
    For this to work, `mnames` must not be a list. Perhaps some of the formulas is long and `deparse` splits it into several strings (60 characters by default). Look at `deparse`'s arguments: `width.cutoff` and `nlines`. – Kamil Bartoń Jan 20 '22 at 12:32
  • Thank you so much Kamil. That worked. I changed `width.cutoff` to be `width.cutoff=100L` and it worked perfectly. I really appreciate your help. – Amanda Goldberg Jan 20 '22 at 15:41

2 Answers2

1

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

Amanda Goldberg
  • 169
  • 1
  • 11
0
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())