R 岭回归模型:glmnet

R 岭回归模型:glmnet,r,machine-learning,regression,glmnet,R,Machine Learning,Regression,Glmnet,在我的训练数据集上使用最小二乘法拟合线性回归模型效果很好 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"

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

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
对于
多项式
二项式
分布,我都有以下错误

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 

您对某些类(如
ftp\u write
只有一个观察值)有单个观察值,这是不允许的(并在错误中明确说明)。

您对某些类(如
ftp\u write
只有一个观察值)有单个观察值,这是不允许的(并在错误中明确说明).

做一个
表(y)
看看是否有计数为0或1的级别。我做了,并且我用
表(y)
的输出编辑了我的问题。错误消息是…?@hong ooi,
lognet中的错误(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,:一个多项式或二项式类有1或0个观察值;不允许
做一个
表(y)
并查看是否有计数为0或1的任何级别。我做了,并且我用
表(y)的输出编辑了我的问题
。错误信息显示…?@hong ooi,
lognet中出现错误(x,is.sparse,ix,jx,y,weights,offset,alpha,nobs,:一个多项式或二项式类有1或0个观测值;不允许
Hello。您建议使用什么R包进行岭回归?glmnet,bigRR,Mass,other?它们中的任何一个都能够处理重复测量(随机效应)?岭回归是一种非常基本的模型,无论您使用什么。只需使用一个具有最简单API的模块即可。岭回归不假设任何关于“重复测量”的内容,因此您会很好(假设您的数据通常正确生成)我的意思是,我想让程序处理固定效应的系数,但我不想让他删除随机效应系数。而且,如果程序考虑到对每个个体采取了多个措施,结果也会不一样。当
cv.glmnet
仅用于2个观测时,这个响应似乎也适用类为
1
的离子和类为
0
的2个观察值。想想看:如果你只对4个样本进行交叉验证,你总是会在一个类中遇到1个样本。你好。你建议使用什么R包进行岭回归?glmnet、bigRR、Mass、other?它们中的任何一个都可以处理重复测量(随机效应)?岭回归是一种非常基本的模型,无论您使用什么。只需使用一个具有最简单API的模块即可。岭回归并没有假设任何关于“重复测量”的内容,因此您会很好(假设您的数据通常正确生成)我的意思是,我想让程序处理固定效应的系数,但我不想让他删除随机效应系数。而且,如果程序考虑到对每个个体采取了多个措施,结果也会不一样。当
cv.glmnet
仅用于2个观测时,这个响应似乎也适用类
1
的离子和类
0
的2个观察值。想想看:如果你交叉验证,只有4个样本,你总是会在一个类中遇到1个样本。
> 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