比较两个向量(预测/预期) [英] Comparing two vectors (predicted/expected)

查看:181
本文介绍了比较两个向量(预测/预期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行接近引导的操作,但是我在处理数据类型时遇到了麻烦.这是脚本:

I am trying to do something close to a shallow bootstrapping but I am struggling with data type. Here is the script :

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"),但是它也不起作用.

I tried to use ifelse (predicted_compsub1 >0.5,"NP","PP") but it doesn't work either.

我收到以下错误:

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.

我可以看到这是类型问题,但是我看不到如何解决该问题. 感谢您的见识!

I can see that it is a matter of type but I fail to see how to fix the problem. Thanks for your insight !

推荐答案

我终于找到了问题所在.我没有在正确的地方使用其他东西:

I finally found what was wrong. I wasn't using if else at the right place :

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")

现在可以使用了!谢谢大家的帮助!

Now it works ! Thanks everyone for your help !

这篇关于比较两个向量(预测/预期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆