Comparing two vectors (predicted/expected)

跟風遠走 提交于 2019-12-13 06:46:55

问题


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)

In the Global Environment window :

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

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

I obtain the following error :

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 !


回答1:


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 !



来源:https://stackoverflow.com/questions/34709106/comparing-two-vectors-predicted-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!