If-else链在R中没有返回正确的结果?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了If-else链在R中没有返回正确的结果?相关的知识,希望对你有一定的参考价值。

所以我最近开始真正研究不同的函数类型,目前正在研究一个函数,这个函数可以接收赛前信息,例如(比赛时间、主场、客场、投注赔率)将这些信息插入预测模型,然后在最后输出预测结果。但是我无法让原for循环里面的if-else语句链输出我想要的正确句子

这是函数的结构。我把实际的模型拿出来,制造了这个问题的结果。模型是在函数外创建的,使用car::predict函数

library(dplyr)
library(sjmisc)
#Here is an example of a data set that would be input into the function
x <- data.frame(home= c("CLE","MIL","DET"),away= c("BOS","IND","OKC"),O_U= c(215.5, 220.5, 209.5),linea= c("+","-","+"),lineb= c(4.0,11.0,8.5),gt= c("2020-02-20 19:00:00","2020-02-20 19:00:00","2020-02-20 19:00:00"))


predictor <- function(x){


gametime <- x[,6]
q <- x[,1] 
w <- x[,2]
OvUn <- x[,3]
linefavor <- x[,4]
spreadtot <- x[,5]


#I took the model out from here and just appended the results onto the end of the x dataframe. The model reproduced this exact table
y <- data.frame(HScore=c(105,114,105),AScore=c(117,106,110))
x <- cbind.data.frame(x,y)

# Here I put them in categories based off of the predictions(1 is true,0 is false,3 is Push)
x <- mutate(x, homewin = ifelse(HScore>AScore,1,0)) 
x <- mutate(x, underdog = ifelse(linefavor == "+",1, ifelse(linefavor == "-",0,"NA")))
x <- mutate(x, Over = ifelse(round(HScore)+round(AScore) > OvUn,1, ifelse(round(HScore)+round(AScore) < OvUn,0,3)))

x <- mutate(x,homecover = ifelse((underdog==1 & (round(HScore)+spreadtot)-round(AScore)>0) | (underdog==0 & (round(HScore)-spreadtot)-round(AScore)>0),1,
                                 ifelse((underdog==1 & (round(HScore)+spreadtot)-round(AScore)==0 | (underdog==0 & (round(HScore)-spreadtot)-round(AScore)==0)),3,0)))

print(x)

#Here is where my results become inaccurate. 
if(homewin ==1 & Over ==1 & homecover ==1){
  return(paste0(w," v ",q,": ",q," win ", round(x$HScore),"-",round(x$AScore),", ",q," cover ",linefavor,spreadtot," spread", " Over ",OvUn))
} else if(homewin ==1 & Over ==1 & homecover ==0){
  return(paste0(w," v ",q,": ",q," win ", round(x$HScore),"-",round(x$AScore),", ",w," cover ",linefavor2,spreadtot," spread", " Over ",OvUn))
} else if(homewin ==1 & Over ==0 & homecover ==0){
  return(paste0(w," v ",q,": ",q," win ", round(x$HScore),"-",round(x$AScore),", ",w," cover ",linefavor2,spreadtot," spread", " Under ",OvUn))
} else if(homewin ==1 & Over ==0 & homecover ==1){
  return(paste0(w," v ",q,": ",q," win ", round(x$HScore),"-",round(x$AScore),", ",q," cover ",linefavor,spreadtot," spread", " Under ",OvUn))
} else if(homewin ==0 & Over ==1 & homecover ==1){
  return(paste0(w," v ",q,": ",w," win ", round(x$AScore),"-",round(x$HScore),", ",q," cover ",linefavor,spreadtot," spread", " Over ",OvUn))
} else if(homewin ==0 & Over ==1 & homecover ==0){
  return(paste0(w," v ",q,": ",w," win ", round(x$AScore),"-",round(x$HScore),", ",w," cover ",linefavor2,spreadtot," spread", " Over ",OvUn))
} else if(homewin ==0 & Over ==0 & homecover ==0){
  return(paste0(w," v ",q,": ",w," win ", round(x$AScore),"-",round(x$HScore),", ",w," cover ",linefavor2,spreadtot," spread", " Under ",OvUn))
} else if(homewin ==0 & Over ==0 & homecover ==1){
  return(paste0(w," v ",q,": ",w," win ", round(x$AScore),"-",round(x$HScore),", ",q," cover ",linefavor,spreadtot," spread", " Under ",OvUn))


} else{
  return("ERROR")
}

  }

#Here is what my result looks like
predictor(x)
#Here is what it should look like
accurate <- c("BOS v CLE: BOS win 117-105, BOS cover -4 spread, Over 215.5","IND v MIL: MIL win 114-106, MIL cover -11 spread, Under 220.5","OKC v DET: OKC win 110-105, DET cover +8.5 spread, Over 209.5")
accurate

这几天我一直在想办法找出问题的根源。

答案

你可以在Google上搜索一下R中的 "向量运算",这是一种不同的思维方式,也是R中的默认思维方式,你不可能用它解决所有问题,所以R还有 for 循环,但你要尽量避免它们。

我对体育博彩的理解还不足以弄清楚谁覆盖或不覆盖什么价差和大球的东西,但我可以让你开始使用一种更简单的方法。

首先,使用 $ 操作符将data.frame中的一列引用为一个向量。 这样一来,你就不必将列分配给不同的变量,而且你的代码也更易读。 作为 x$home您也可以使用 x[[1]]x[["home"]] (注意: x["home"] -- 单括号--返回一个单列的data.frame,而不是一个向量)。) 我喜欢使用列名而不是列号,这样即使将来列的顺序改变了,我的代码仍然可以使用。

使用 x 从你的例子中的data.frame,我会通过建立字符向量来解决这个问题,这些字符向量应该在你所需输出的每个位置。 例如

winningTeam = ifelse(df$HScore > df$AScore, as.character(df$home), as.character(df$away))
winScore = pmax(x$HScore, x$AScore)
loseScore = pmin(x$HScore, x$AScore)

产量。

> winningTeam
[1] "BOS" "MIL" "OKC"
> winScore
[1] 117 114 110
> loseScore
[1] 105 106 105

你可以用同样的方法来创建向量来计算谁的赔率,赔率是多少,弱队等等。

然后,由于 paste 函数处理向量,就像 paste 一起的向量。

result = paste0(x$home, " v ", x$away, ": ", winningTeam, " win ", winScore, "-", loseScore)

结果是:

> result
[1] "CLE v BOS: BOS win 117-105" "MIL v IND: MIL win 114-106" "DET v OKC: OKC win 110-105"

以上是关于If-else链在R中没有返回正确的结果?的主要内容,如果未能解决你的问题,请参考以下文章

为啥此代码片段返回意外结果?

Relay.js 没有正确解析组合片段

findViewById(R.id.videoView) 在片段中返回 Null (Android Studio)

20个简洁的 JS 代码片段

20个简洁的 JS 代码片段

mysql没有返回正确的json