比较两个向量(预测/预期)

时间:2016-01-10 18:19:18

标签: r logistic-regression prediction glm

我正在尝试接近引导,但我正在努力处理数据类型。这是脚本:

library(languageR)
data(dative)
sub1<-dative[grepl("S10|S11",dative$Speaker),]
mod_sub1<-glm(RealizationOfRecipient~Verb+SemanticClass+LengthOfRecipient+AnimacyOfRec+DefinOfRec+PronomOfRec+LengthOfTheme+AnimacyOfTheme+DefinOfTheme+PronomOfTheme+AccessOfRec+AccessOfTheme,family='binomial',data=sub1)
comp_sub1<-dative[!grepl("S10|S11",dative$Speaker),]
expected_compsub1  <- comp_sub1$RealizationOfRecipient
predicted_compsub1 <- predict(mod_sub1,ndata=comp_sub1,type="response")
predictions_sub1   <- prediction(predicted_compsub1,expected_compsub1)
performance_sub1   <- performance(predictions_sub1,"tpr","fpr")
plot(performance_sub1)

全球环境窗口中:

- expected_compsub1 : Factor w/ 2 levels "NP","PP" : 1 1 1 ...
- predicted_compsub1 : Named num [1:1076] 0.1561 0.9889 ...

我尝试使用ifelse (predicted_compsub1 >0.5,"NP","PP"),但它也不起作用。

我获得了以下错误

predictions_sub1   <- prediction(y_predicted_compsub1,expected_compsub1)
Error in prediction(y_predicted_compsub1, expected_compsub1) : 
Number of predictions in each run must be equal to the number of labels for each run.

我可以看到这是一个类型的问题,但我没有看到如何解决问题。 感谢您的见解!

1 个答案:

答案 0 :(得分:1)

我终于发现了什么问题。我没有使用if else在正确的地方:

library(languageR)
library(ROCR)
data(dative)    
sub1<-dative[grepl("S10|S11",dative$Speaker),]
complementaire_sub1<-dative[!grepl("S10|S11",dative$Speaker),]
mod_sub1<-glm(RealizationOfRecipient~LengthOfRecipient+AnimacyOfRec+DefinOfRec+PronomOfRec+LengthOfTheme+AnimacyOfTheme+DefinOfTheme+PronomOfTheme+AccessOfRec+AccessOfTheme,family='binomial',data=complementaire_sub1) # minus subjects,verbs
expected_compsub1  <- sub1$RealizationOfRecipient
predicted_compsub1 <- predict(mod_sub1,newdata=sub1,type="response")
predicted_compsub1 <- ifelse(predicted_compsub1 > 0.5,0,1)
predictions_sub1   <- prediction(predicted_compsub1,expected_compsub1)
performance_sub1   <- performance(predictions_sub1,"tpr","fpr")
sum(predicted_compsub1 & as.numeric(expected_compsub1))/sum(as.numeric(expected_compsub1))
sum(predicted_compsub1 & as.numeric(expected_compsub1))/sum(predicted_compsub1)
plot(performance_sub1,main="S10|S11")

现在它有效!谢谢大家的帮助!