簡體   English   中英

“ glm”和“ rf”中的調整參數

[英]The tuning parameter in “glm” vs “rf”

我正在嘗試在train使用method = "glm"建立分類模型。 當我使用method = "rpart"它可以正常工作,但是當我切換到method = "glm"它給了我一個錯誤提示

調整參數網格應具有列參數

我嘗試使用

cpGrid = data.frame(.0001) 

cpGrid = data.frame(expand.grid(.cp = seq(.0001, .09, .001)))

但是兩者都拋出錯誤。
下面是我的初始代碼

numFolds = trainControl(method = "cv", number = 10, repeats = 3)
cpGrid = expand.grid(.cp = seq(.0001, .09, .001))

工作正常

temp <-train(Churn. ~., data = train, method = 'rpart', trControl = numFolds, tuneGrid = cpGrid)

給出錯誤

treeCV <-train(Churn. ~., data = train, method = 'glm', trControl = numFolds, tuneGrid = data.frame(cpGrid))
predictCV = predict(treeCV, newdata = test, type = "prob")

從我的數據中dput

train <- structure(list(State = structure(c(17L, 32L, 36L, 37L, 20L, 25L
), .Label = c("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", 
"DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", 
"MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", 
"NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", 
"SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY"), class = "factor"), 
    VMail.Message = c(25L, 0L, 0L, 0L, 24L, 0L), Day.Mins = c(265.1, 
    243.4, 299.4, 166.7, 218.2, 157), Day.Calls = c(110L, 114L, 
    71L, 113L, 88L, 79L), Eve.Charge = c(16.78, 10.3, 5.26, 12.61, 
    29.62, 8.76), Night.Mins = c(244.7, 162.6, 196.9, 186.9, 
    212.6, 211.8), Night.Calls = c(91L, 104L, 89L, 121L, 118L, 
    96L), Intl.Mins = c(10, 12.2, 6.6, 10.1, 7.5, 7.1), CustServ.Calls = c(1L, 
    0L, 2L, 3L, 3L, 0L), Churn. = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("False.", "True."), class = "factor"), 
    Area.Code = c(2, 2, 1, 2, 3, 2), Int.l.Plan = c(1, 1, 2, 
    2, 1, 2), VMail.Plan = c(2, 1, 1, 1, 2, 1), Day.Charge = c(565, 
    1005, 1571, 665, 1113, 580), Eve.Mins = c(690, 87, 1535, 
    256, 1517, 9), Eve.Calls = c(120, 12, 109, 25, 10, 115), 
    Night.Charge = c(101, 644, 797, 753, 866, 862), Intl.Calls = c(15, 
    17, 19, 15, 19, 15), Intl.Charge = c(78, 100, 44, 79, 53, 
    49)), .Names = c("State", "VMail.Message", "Day.Mins", "Day.Calls", 
"Eve.Charge", "Night.Mins", "Night.Calls", "Intl.Mins", "CustServ.Calls", 
"Churn.", "Area.Code", "Int.l.Plan", "VMail.Plan", "Day.Charge", 
"Eve.Mins", "Eve.Calls", "Night.Charge", "Intl.Calls", "Intl.Charge"
), row.names = c(1L, 3L, 4L, 5L, 7L, 8L), class = "data.frame")

需要您的幫助才能在method = "glm"使用cpGrid 。還想知道我應該如何在其中包括ntree 我瀏覽了這里和那里提供的一些解決方案,但是似乎沒有任何效果。

caretmodelLookup命令提供與模型調整參數有關的信息。
對於rpart只有一個調整參數可用,即cp復雜度參數。

modelLookup("rpart")

#################
  model parameter                label forReg forClass probModel
1 rpart        cp Complexity Parameter   TRUE     TRUE      TRUE

glm的調整參數是parameter (我不知道它的用途):

modelLookup("glm")

#################
  model parameter     label forReg forClass probModel
1   glm parameter parameter   TRUE     TRUE      TRUE

因此, tuneGrid for glm需要一個名為.parameter的列:

glmGrid = expand.grid(.parameter = seq(1, 10, 1))
glmCV <- train(Churn. ~., data = train, method = 'glm', trControl = numFolds, 
      tuneGrid = data.frame(glmGrid))

predictCV = predict(glmCV, newdata = test, type = "prob")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM