Hello如何自动化变量名称以将其更改为其他变量。
#Variable names
d <- data.frame(Bound_Count = 1, Days_diff_Eff_Subm_2 = 2, TR_BS_BROKER_ID_360_M = 3,
TR_BS_BROKER_ID_360_2 = 4, RURALPOP_P_CWR_2 = 5, RURALPOP_P_CWR_M = 6, PIP_Flag = 7,
TR_B_BROKER_ID_360 = 8, TR_SCW_BROKER_ID_360 = 9, TR_BS_BROKER_INDIVIDUAL_720_2 = 10,
TR_BS_BROKER_INDIVIDUAL_720_M = 11, Resolved_Conflict = 12,
Priority_2 = 13)
# exclude dependent variable
allvariables <- names(d)[-1]
# container for models
listOfModels <- vector("list", length(allvariables))
# loop over variables
for (i in seq_along(allvariables)) {
# exclude variable i
currentvariable <- allvariables[-i]
# programmatically assemble regression formula
regressionFormula <- as.formula(
paste("Bound_Count ~ ", paste(currentvariable, collapse="+")))
# fit model
currentModel <- glm(formula = regressionFormula, family=binomial(link = "logit"), data=dataL_TT)
# store model in container
listOfModels[[i]] <- currentModel
}
listOfModels
所以基本上我只想自动化d&lt; - data.frame来输入/替换其中的其他变量。不只是通过手动将它们中的每一个替换为新变量。 我一直在尝试下面的代码,但它不起作用。
#Automate variable names trial
Y <- dataL$Bound_Count
X1 <- dataL$Days_diff_Eff_Subm_2
X2 <- dataL$TR_BS_BROKER_ID_360_M
X3 <- dataL$TR_BS_BROKER_ID_360_2
X4 <- dataL$RURALPOP_P_CWR_2
X5 <- dataL$RURALPOP_P_CWR_M
X6 <- dataL$PIP_Flag
X7 <- dataL$TR_B_BROKER_ID_360
X8 <- dataL$TR_SCW_BROKER_ID_360
X9 <- dataL$TR_BS_BROKER_INDIVIDUAL_720_2
X10 <- dataL$TR_BS_BROKER_INDIVIDUAL_720_M
X11 <- dataL$Resolved_Conflict
X12 <- dataL$Priority_2
d <- data.frame(Y = 1, X1 = 2, X2 = 3,
X3= 4, X4 = 5, X5 = 6, X6 = 7,
X7 = 8, X8 = 9, X9 = 10,
X10 = 11, X11 = 12,
X12 = 13)
# exclude target variable
allvariables <- names(d)[-1]
# container for models
listOfModels <- vector("list", length(allvariables))
# loop over variables
for (i in seq_along(allvariables)) {
# exclude variable i
currentvariable <- allvariables[-i]
# programmatically assemble regression formula
regressionFormula <- as.formula(
paste("Bound_Count ~ ", paste(currentvariable, collapse="+")))
# fit model
currentModel <- glm(formula = regressionFormula, family=binomial(link = "logit"), data=dataL_TT)
# store model in container
listOfModels[[i]] <- currentModel
}
listOfModels
或者您有其他想法吗?