[英]how to select variables to use them in a formula with R
我想做一個可以輕松運行多個模型的功能。 只有使用的模型輸入變量不同。 我為此模型使用rpart函數。 理想情況下,我有一個包含模型及其變量的表(命名為變量)。 看起來像這樣的東西
model1 model2 model3 …………………
gender gender age
age education wageparents
education nfriends
married
比起我可以插入fun(數據,變量)的函數
到目前為止,我使用的是:
tree <-rpart(wage ~ gender + age + education, method='class', data=Data, control=rpart.control(minsplit=1, minbucket=1, cp=0.002))
這可行,但是我每次都要更改模型公式
我嘗試過類似的方法,但是我不確定必須使用哪種數據類型等。
wagefun <- function(Data, variables$model1){
tree <-rpart(wage ~ variables$model1, method='class', data=Data, control=rpart.control(minsplit=1, minbucket=1, cp=0.002))
return(tree)
}
創建一個公式reformulate
:
form <- reformulate(termlabels = variables$model1, response = "wage", intercept = TRUE)
rpart(form, ...)
注意到目前為止您已經忽略的截距術語:這是一個附加的建模選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.