Comparing two vectors (predicted/expected)

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 !

Upvotes: 0

Views: 257

Answers (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 !

Upvotes: 1

Related Questions