R:训练和测试集对类别解释变量:逻辑回归采用不同的值

时间:2018-11-05 04:14:30

标签: r machine-learning statistics logistic-regression categorical-data

我正在尝试预测遗传变异所属的类别。我的数据框在我的代码中称为遗传。我将数据框按如下方式分为训练和测试数据集:

set.seed(1)
train=sample(54248,27124)
test=-train
Genetictrain=Genetic[train,]
Genetictest=Genetic[test,]

问题是我的一个解释变量(属于类别,数据框的列之一)在训练集(Genetictrain)和测试集(Genetictest)中具有不同的值。说明性变量称为Genetic $ Consequence。遗传结果的级别为:

 [1] "3_prime_UTR_variant"                                           
 [2] "5_prime_UTR_variant"                                           
 [3] "downstream_gene_variant"                                       
 [4] "frameshift_variant"                                            
 [5] "frameshift_variant&splice_region_variant"                      
 [6] "frameshift_variant&start_lost"                                 
 [7] "frameshift_variant&start_lost&start_retained_variant"          
 [8] "frameshift_variant&stop_lost"                                  
 [9] "frameshift_variant&stop_retained_variant"                      
[10] "inframe_deletion"                                              
[11] "inframe_deletion&splice_region_variant"                        
[12] "inframe_insertion"                                             
[13] "inframe_insertion&splice_region_variant"                       
[14] "intergenic_variant"                                            
[15] "intron_variant"                                                
[16] "intron_variant&non_coding_transcript_variant"                  
[17] "missense_variant"                                              
[18] "missense_variant&splice_region_variant"                        
[19] "protein_altering_variant"                                      
[20] "splice_acceptor_variant"                                       
[21] "splice_acceptor_variant&coding_sequence_variant"               
[22] 
"splice_acceptor_variant&coding_sequence_variant&intron_variant"
[23] "splice_acceptor_variant&intron_variant"                        
[24] "splice_donor_variant"                                          
[25] "splice_donor_variant&coding_sequence_variant"                  
[26] "splice_donor_variant&coding_sequence_variant&intron_variant"   
[27] "splice_donor_variant&intron_variant"                           
[28] "splice_region_variant&3_prime_UTR_variant"                     
[29] "splice_region_variant&5_prime_UTR_variant"                     
[30] "splice_region_variant&coding_sequence_variant&intron_variant"  
[31] "splice_region_variant&intron_variant"                          
[32] "splice_region_variant&synonymous_variant"                      
[33] "start_lost"                                                    
[34] "start_lost&5_prime_UTR_variant"                                
[35] "start_lost&splice_region_variant"                              
[36] "stop_gained"                                                   
[37] "stop_gained&frameshift_variant"                                
[38] "stop_gained&inframe_deletion"                                  
[39] "stop_gained&inframe_insertion"                                 
[40] "stop_gained&protein_altering_variant"                          
[41] "stop_gained&splice_region_variant"                             
[42] "stop_lost"                                                     
[43] "stop_lost&3_prime_UTR_variant"                                 
[44] "stop_retained_variant"                                         
[45] "stop_retained_variant&3_prime_UTR_variant"                     
[46] "synonymous_variant"   
[47] "TF_binding_site_variant"                                       
[48] "upstream_gene_variant"  

但是:当我对训练数据(Genetictrain)进行逻辑回归时,出现错误:

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  factor Consequence has new levels frameshift_variant&stop_retained_variant, protein_altering_variant, splice_acceptor_variant&coding_sequence_variant, start_lost&splice_region_variant, stop_retained_variant&3_prime_UTR_variant

我用于逻辑回归的代码是:

Logisticfit=glm(CLASS~AF_TGP + Consequence + CHROM + AF_ESP+STRAND + AF_EXAC + CADD_RAW + LoFtool + CADD_PHRED,data=Genetictrain,family="binomial")
LogisticProb=predict(Logisticfit,Genetictest,type="response")

错误结果(使用上面的预测函数运行代码)是因为训练集,Generictrain没有出现任何后果的蛋白质改变变体,但是Genetictest确实发生了后果的蛋白质改变变体:

which(Genetictrain$Consequence=="protein_altering_variant")
integer(0)
 which(Genetictest$Consequence=="protein_altering_variant")
[1] 10720

与错误引起的其他值相同。

是否有任何方法可以避免这种情况,以便我可以运行预测函数而不会收到错误消息(请注意,我的解释变量既是类别变量又是连续变量,并且我正在尝试预测二进制0或1的CLASS)?结果对我来说是一个重要的解释变量,因此我不想删除它。
谢谢!

1 个答案:

答案 0 :(得分:1)

现在检查您的数据框。数据集出现问题不匹配

火车数据集和测试数据集在Genetic$consequence中没有相同的信息。

检查以下代码:

data.frame(table(Genetic$Consequence))%>%setNames(.,c("Consequnce","Freq"))%>%arrange(Freq)

输出:

                                                       Consequnce  Freq
1            frameshift_variant&start_lost&start_retained_variant     1
2                        frameshift_variant&stop_retained_variant     1
3                         inframe_insertion&splice_region_variant     1
4                    intron_variant&non_coding_transcript_variant     1
5    splice_region_variant&coding_sequence_variant&intron_variant     1
6                                  start_lost&5_prime_UTR_variant     1
7                                    stop_gained&inframe_deletion     1
8                                   stop_gained&inframe_insertion     1
9                            stop_gained&protein_altering_variant     1

频率有9种结果,因为1表示如果u分割将进入训练或测试数据集中的数据帧。

示例 比如说“ frameshift_variant&start_lost&start_retained_variant”仅在Genericdata $结果中有一行,所以当您划分数据帧时,它将进入训练或测试数据集中。如果火车数据集中的那一行,则测试数据集中没有任何行。为此,它只会返回错误。

解决方案: 尝试使用1获得更多的频率变量(意味着仅存在一行,因此在一列火车中至少需要2行,在测试数据集中至少需要2行)  要么 U可以像低频率那样对数据集进行子集化,以便在训练和测试数据集中轻松获得信息。