R语言 创建包含型号的AIC型号选择表

31moq8wy  于 2023-02-27  发布在  其他
关注(0)|答案(2)|浏览(141)

我一直在使用如下所述的代码:How to Create AIC Model Selection Table in R in LaTex format?
然而,我最近的数据集遇到了一个错误,我认为这与模型项的数量较多有关(我有11列模型项)
下面是我的代码:

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])

一切看起来都很好,直到我看到'mnames',其中一些模型看起来像这样:
19亿美元
[1]"存活率~注射 * 年份+治疗 * 年份+治疗 * 性别+注射 * 治疗+""注射 * 性别+年份 * 性别"
当我运行最后一行代码时,我得到了这个错误:Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 113
我想问题是mnames中的"",但我不知道为什么会这样。我试着缩短名称,看看是否是因为大小,但似乎没有工作(虽然可能我没有缩短所有的东西足够)。我也试着删除'simplify. formula',以防它是由于,但仍然是同样的问题。有什么想法,我可以解决这个问题?
谢谢
回应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
bzzcjhmw

bzzcjhmw1#

为了向那些对此问题感兴趣的人澄清,我只需要将width.cutoff=100L添加到mnames代码行中。

# 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))

它现在工作得很完美

vecaoik1

vecaoik12#

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())

相关问题