【问题标题】:Ridge-regression model: glmnet岭回归模型:glmnet
【发布时间】:2016-06-28 16:47:20
【问题描述】:

在我的训练数据集上使用最小二乘拟合线性回归模型效果很好。

library(Matrix)
library(tm)
library(glmnet)
library(e1071)
library(SparseM)
library(ggplot2)

trainingData <- read.csv("train.csv", stringsAsFactors=FALSE,sep=",", header = FALSE)
testingData  <- read.csv("test.csv",sep=",", stringsAsFactors=FALSE, header = FALSE)

lm.fit = lm(as.factor(V42)~ ., data = trainingData)
linearMPrediction = predict(lm.fit,newdata = testingData, se.fit = TRUE)
mean((linearMPrediction$fit - testingData[,20:41])^2) 
linearMPrediction$residual.scale

但是,当我尝试在我的训练数据集上拟合岭回归模型时,

x = model.matrix(as.factor(V42)~., data = trainingData) 
y = as.factor(trainingData$V42) 
ridge = glmnet(x, y, family = "multinomial", alpha = 1, lambda.min.ratio = 1e-2)

multinomialbinomial 发行版都出现以下错误。

Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs,  : 
  one multinomial or binomial class has 1 or 0 observations; not allowed

我错过了什么吗?任何评论将不胜感激。顺便说一下,这是我的数据的一部分。

> trainingData$V42[1:50]
 [1] "normal"      "normal"      "neptune"     "normal"      "normal"      "neptune"     "neptune"     "neptune"     "neptune"     "neptune"     "neptune"    
[12] "neptune"     "normal"      "warezclient" "neptune"     "neptune"     "normal"      "ipsweep"     "normal"      "normal"      "neptune"     "neptune"    
[23] "normal"      "normal"      "neptune"     "normal"      "neptune"     "normal"      "normal"      "normal"      "ipsweep"     "neptune"     "normal"     
[34] "portsweep"   "normal"      "normal"      "normal"      "neptune"     "normal"      "neptune"     "neptune"     "neptune"     "normal"      "normal"     
[45] "normal"      "neptune"     "teardrop"    "normal"      "warezclient" "neptune"  

> x
      (Intercept)    V1 V2tcp V2udp V3bgp V3courier V3csnet_ns V3ctf V3daytime V3discard V3domain V3domain_u V3echo V3eco_i V3ecr_i V3efs V3exec V3finger V3ftp
1               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
2               1     0     0     1     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
3               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
4               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
5               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
6               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
7               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
8               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
9               1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0
10              1     0     1     0     0         0          0     0         0         0        0          0      0       0       0     0      0        0     0

> y[1:50]
 [1] normal      normal      neptune     normal      normal      neptune     neptune     neptune     neptune     neptune     neptune     neptune     normal     
[14] warezclient neptune     neptune     normal      ipsweep     normal      normal      neptune     neptune     normal      normal      neptune     normal     
[27] neptune     normal      normal      normal      ipsweep     neptune     normal      portsweep   normal      normal      normal      neptune     normal     
[40] neptune     neptune     neptune     normal      normal      normal      neptune     teardrop    normal      warezclient neptune    
22 Levels: back buffer_overflow ftp_write guess_passwd imap ipsweep land loadmodule multihop neptune nmap normal phf pod portsweep rootkit satan smurf spy ... warezmaster

> table(y)
y
           back buffer_overflow       ftp_write    guess_passwd            imap         ipsweep            land      loadmodule        multihop         neptune 
            196               6               1              10               5             710               1               1               2            8282 
           nmap          normal             phf             pod       portsweep         rootkit           satan           smurf             spy        teardrop 
            301           13449               2              38             587               4             691             529               1             188 
    warezclient     warezmaster 
            181               7 

【问题讨论】:

  • 执行table(y) 看看是否有任何级别的计数为 0 或 1。
  • 我做了并且我已经用table(y)的输出编辑了我的问题。
  • 错误信息说...?
  • @hong-ooi, Error in lognet(x, is.sparse, ix, jx, y, weights, offset, alpha, nobs, : one multinomial or binomial class has 1 or 0 observations; not allowed

标签: r machine-learning regression glmnet


【解决方案1】:

您对某些类有单一的观察(例如 ftp_write 只有 1 个观察),这是不允许的(并在错误中明确说明)。

【讨论】:

  • 你好。您建议使用什么 R 包进行岭回归? glmnet,bigRR,质量,其他?他们中的任何一个能够处理重复测量(随机效应)吗?
  • 岭回归是如此基本的模型,你使用什么都没有关系。只需使用一个为您提供最简单 API 的模块。岭回归不假设任何关于“重复测量”的内容,因此您会很好(假设您的数据通常是正确生成的)
  • 我的意思是我希望程序使用固定效应的系数,但我不希望他删除随机效应系数。此外,如果程序考虑到对每个人采取了多项措施,结果也不会相同
  • cv.glmnet 仅用于1 类的2 个观察和0 类的2 个观察时,此响应似乎也适用。想一想:如果您只对 4 个样本进行交叉验证,那么您总是会遇到在一个类中有 1 个样本。
猜你喜欢
  • 2018-07-19
  • 2018-12-03
  • 2017-12-05
  • 2019-07-03
  • 2019-09-19
  • 2012-10-07
  • 2022-07-15
  • 2018-11-17
  • 2019-07-13
相关资源
最近更新 更多