R:将其编写为函数的变量排名模型自动化代码

标签 r

如何将下面的命令列表写入一个函数中?

例如:VariableRanking <- function(formula, variables,.....) { 插入命令.........

#Variable Ranking Model automation
#exclusion of the variables that are not model variables
exclude <- c("~,", "+" ) # exclude target which is bound_count for Property
formula <- toString(formula)
formula

#listing the entire model formula out
variables_pre <- unlist(strsplit(formula, split = " "))
variables_pre

#keeping only the model variables
variables <-  sort(variables_pre[!variables_pre %in% exclude])
variables

#Exclude "," on the target variable 
variables[1] <- substr(variables[1], 1, nchar(variables[1])-1)
variables

#Assigning the variables into a data frame
d <- c(1:length(variables))
d
d= data.frame(d)
d
d= t(d)
d
colnames(d)=variables
d

# exclude target variable on the data frame
allvariables <- colnames(d)[-1]
allvariables
# container for models
listOfModels <- vector("list", length(allvariables))
listOfModels
# loop over variables
for (i in seq_along(allvariables)) {
  # exclude variable i
  currentvariable <- allvariables[-i]
  # programmatically assemble regression formula
  regressionFormula <- as.formula(
    paste(variables[1],"~", paste(currentvariable, collapse="+")))
  # fit model
  currentModel <- glm(formula = regressionFormula, family=binomial(link = "logit"), data=dataL_TT)
  # store model in container
  listOfModels[[i]] <- currentModel
} 
listOfModels

#List of AICs for each model 
lapply(listOfModels,function(xx) xx$aic)

#Assign X as the AIC of the full model
X <- modelTT$aic
X

# Difference of AICs of each model to the AIC of the full model
AICdifference <- lapply(listOfModels,function(xx) xx$aic - X)
AICdifference

# Naming the AIC Difference
AICdifference2 = data.frame(variables=allvariables, AICdiff=unlist(AICdifference))
AICdifference2

#Graph the Barchart of the AIC decrease of each variables and save it to pdf

pdf("Barchart.pdf",width=12,height=10)
par(mar=c(2,18,2,5))

barplot(sort(AICdifference2$AICdiff, decreasing = F), main="Variable Ranking based on AIC decrease", 
        horiz=TRUE, xlab="AIC Increase", names.arg= AICdifference2$variables[order(AICdifference2$AICdiff, decreasing = F)], 
        las=1, col= 'dodgerblue4')

dev.off()

这可能吗?因为它有很多参数。 所以基本上我只需要 AICdifference2 数据框的输出。 并将条形图另存为pdf并弹出

最佳答案

试试这个:

FOO <- function(myformula, data, fullmodel_AIC, plotname){

  exclude <- c("~,", "+" ) # exclude target which is bound_count for Property
  myformula <- toString(myformula)

  variables_pre <- unlist(strsplit(myformula, split = " "))
  variables <-  sort(variables_pre[!variables_pre %in% exclude])
  variables[1] <- substr(variables[1], 1, nchar(variables[1])-1)

  d <- t(data.frame(c(1:length(variables))))
  colnames(d)=variables

  allvariables <- colnames(d)[-1]

  listOfModels <- vector("list", length(allvariables))

  for (i in seq_along(allvariables)) {
    # exclude variable i
    currentvariable <- allvariables[-i]
    # programmatically assemble regression formula
    regressionFormula <- as.formula(
      paste(variables[1],"~", paste(currentvariable, collapse="+")))
    # fit model
    currentModel <- glm(formula = regressionFormula, family=binomial(link = "logit"), data = data)
    # store model in container
    listOfModels[[i]] <- currentModel
  } 

  AICdifference <- lapply(listOfModels,function(xx) xx$aic - fullmodel_AIC)
  AICdifference2 <- data.frame(variables=allvariables, AICdiff=unlist(AICdifference))

  pdf(paste0(plotname, ".pdf"),width=12,height=10)
  par(mar=c(2,18,2,5))

  barplot(sort(AICdifference2$AICdiff, decreasing = F), main="Variable Ranking based on AIC decrease", 
          horiz=TRUE, xlab="AIC Increase", names.arg= AICdifference2$variables[order(AICdifference2$AICdiff, decreasing = F)], 
          las=1, col= 'dodgerblue4')

  dev.off()

  return(AICdifference2)
}

您需要四个参数:myformuladata(代码中的dataL_TT)、fullmodel_AIC (modelTT$aic 在你的代码中),以及一个字符串来命名你的情节。

尝试使用 FOO(myformula, dataL_TT, modelTT$aic, "test") 调用它并为 myformula 插入公式对象。

我已经将 formula 更改为 myformula 因为 formula 是 stats 包的基本函数,使用 object 通常是不明智的作为基本函数的名称。

关于R:将其编写为函数的变量排名模型自动化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782281/

相关文章:

r - 是否有 R 脚本可以使用 Excel 电子表格中的内容编写 rmd 文件?

r - 关于 R 中的词法范围

r - 如何在 expression() 中使用 atop 函数?

r - 如何检查某些列是否存在,如果不存在,如何创建它们并用零填充它们?

r - 在内部服务器上设置 Shiny 应用程序

r - 使用具有自定义功能的ddply + mutate吗?

r - R中ggplot2中的分组条形图

重新调整 y 轴 ggplot (geom_bar)

r - 如何在.Rprofile中存储API key ?

Rlang:如何将字符串视为符号