【问题标题】:Comparing two vectors (predicted/expected)比较两个向量(预测/预期)
【发布时间】:2016-01-10 18:19:17
【问题描述】:

我正在尝试做一些接近于 shallow 引导的事情,但我正在为数据类型而苦苦挣扎。这是脚本:

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 &gt;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.

我可以看出这是类型问题,但我不知道如何解决问题。 感谢您的洞察力!

【问题讨论】:

  • predict() 函数从何而来?它没有加载那个包。
  • 通常的predict 函数,如predict.glm 采用type 参数。您指定 type = "response" 以获取预测的 y 值,而不是线性预测器。查看prediction 函数的文档 - 无论它来自什么包 - 看看它是否有类似的东西。
  • prediction() 来自 ROCR 库。在文档中,它的工作方式如下: prediction(predictions, labels, label.ordering = NULL) ...我看不出它与“类型”(我在 predict() 中使用)的关系。
  • @Parselmouthintraining : 那为什么 ROCR 没有库调用???
  • @42 : 脚本写得比较高,我忘记复制了...

标签: r logistic-regression prediction glm


【解决方案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")

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

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 2010-12-25
    • 2018-01-21
    • 2018-09-16
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多