首页 > 解决方案 > 使用插入符号运行逻辑回归时出现错误消息

问题描述

我收到错误消息

Error: wrong model type for classification

尝试运行以下代码进行逻辑回归时。我应该改变什么?

lm1 <- train(Class ~ .,
        method = "lm", 
        trControl = tr.Control, 
        data = train.dat)

数据集:

structure(list(x1 = c(-2.48941991263215, -0.338448010439568, 
-1.07796826066294, 1.47833943928667, -0.19013864138727), x2 = c(-1.05660014431803, 
-1.75938416652951, -1.94445363537753, 2.65603302304451, -0.818464313993987
), x3 = c(-0.928819609794076, -0.24431689960579, -0.26055539595143, 
-0.500006066823682, 0.19947842697796), x4 = c(0.167674885884102, 
-0.714651010370962, 0.501841366660604, -0.261356553409404, -0.121081806911108
), x5 = c(0.826293680351228, -0.0522530856542289, 0.456970179919153, 
-0.483860304113477, 0.827117071952671), x6 = c(0.229410925647244, 
0.367363323224708, 0.0097867208532989, 0.6599692159798, 0.454895325470716
), x7 = c(0.277445634594187, 0.00411403737962246, 0.912381467409432, 
0.0911673668306321, 0.0729619956109673), x8 = c(0.403632419444111, 
-1.76177968998027, 0.818339220424296, 0.77257524859948, -1.45634200383022
), x9 = c(0.666298305218494, 1.28068782733132, 0.243489971387096, 
0.00907678612957343, 0.0688231437305274), x10 = c(-0.674113519037765, 
-0.221583500325269, 0.555570222138564, 0.572105515491289, 2.32224808146226
), x11 = c(-0.503906052691753, -0.170463238913734, 1.81239693119702, 
-0.310259330876175, 0.373355276436323), x12 = c(0.569346066655445, 
0.665270271264321, -1.04590277174209, -1.08749423169221, -0.717326819631265
), Class = c("No", "Yes", "Yes", "No", "Yes")), row.names = c(NA, 
5L), class = "data.frame")

标签: rregressionr-caret

解决方案


使用插入符号method = "lm"调用lm()作为线性回归的函数。当您的响应变量不是标量时,您需要一个通用线性模型来实现所谓的逻辑回归。在插入符号中,这是通过指定method = "glm"for 来完成的:

library(caret)

tr.Control = trainControl(method="cv")

lm1 <- train(Class ~ .,
        method = "glm", 
        trControl = tr.Control, 
        data = train.dat)

您提供的数据是秩亏矩阵,请在caret再次运行前检查。


推荐阅读