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

If-else chain not returning correct results in 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

最近几天我一直在努力找出问题的根源。

您可能想在 R 中搜索 Google "vector operations"。这是一种不同的思维方式,也是 R 中的默认思维方式。您无法解决所有问题它,所以 R 仍然有 for 循环,但你想尽量避免它们。

我对体育博彩的了解不足以弄清楚谁涵盖或不涵盖什么点差和 over/under 东西,但我可以让你开始使用更简单的方法。

首先,使用 $ 运算符将 data.frame 中的列作为向量引用。这样,您不必将列分配给不同的变量,并且您的代码更具可读性。作为 x$home 的替代方法,您还可以使用 x[[1]]x[["home"]]。 (请注意 x["home"] -- 单括号 -- returns 单列 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"