Итак, я недавно начал серьезно разбираться в различных типах функций и в настоящее время работаю над функцией, которая принимает информацию перед игрой, например (время игры, дома, на выезде, ставки ставок), вставляет эту информацию в прогнозную модель, а затем выводит прогнозы в конце. Но я не могу получить цепочку операторов if-else внутри оригинала для l oop, чтобы вывести нужное мне предложение.
Вот структура функции. Я взял реальную модель и представил результаты по этому вопросу. Модель создается вне функции и использует функцию car :: predic
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
Последние несколько дней я был в тупике, пытаясь выяснить, где источник проблемы.