以下代码是我用来进行赌博预测的内容,第一部分是我用来抓取结果和固定装置的内容,第二部分是我用来实际进行预测的内容。我好奇的是,实际上是代码的第二部分。
Probabilities <- ProbTable(TeamParameters,"Augsburg","Freiburg")
ResultProbabilities<- ResultProbs(Probabilities)
ResultProbabilities
cat("\nHome Win:", 100/ResultProbabilities$HomeWin)
cat("\nDraw:", 100/ResultProbabilities$Draw)
cat("\nAway Win:", 100/ResultProbabilities$AwayWin)
与其手动输入足球队,不如示例中的“奥格斯堡”和“弗莱堡”所示。运行head(Fix)时是否可以从“ Fix”(特别是6个灯具)中提取数据,因此在此示例中为
X1 X2 X3 X4 X5
1 05.10.2018 Werder Bremen 18:30 Wolfsburg NA
2 06.10.2018 Mainz 05 13:30 Hertha BSC NA
3 06.10.2018 Hannover 96 13:30 Stuttgart NA
4 06.10.2018 Borussia Dortmund 13:30 Augsburg NA
5 06.10.2018 Fortuna Düsseldorf 13:30 Schalke 04 NA
6 06.10.2018 Bayern München 16:30 Borussia M'gladbach NA
是否可以做一些事情来同时运行它们,而不是手动进入每个团队?我只需要X2和X4的团队就可以到达“奥格斯堡”和“弗莱堡”的位置。
代码第1部分。
library(rvest)
library(stringr)
library(plyr)
library(dplyr)
library(ggvis)
library(knitr)
library(XML)
library(tidyr)
url <- "https://www.betstudy.com/soccer-stats/c/portugal/primeira-liga//d/results/"
webpage <- read_html(url)
fixtures <- "http://www.betstudy.com/soccer-stats/c/portugal/primeira-liga//d/fixtures/"
wpfix <- read_html(fixtures)
fixtable <- html_nodes(wpfix, 'table')
Fix <- html_table(fixtable)[[2]]
Champ_Table <- html_nodes(webpage, 'table')
Champ<- html_table(Champ_Table,fill = TRUE)[[2]]
names(Champ) <- c("Date", "HomeTeam", "Score"," AwayTeam", "delete")
Champ$delete <- NULL
head(Champ)
Champ <- Champ[!grepl('Pstp',Champ$Score),]
Champ$Date <- NULL
Champ <- separate(Champ,Score, into = c("FTHG", "FTAG"), sep="-")
Champ <- Champ[,c(1,4,2,3)]
write.csv(Champ, file="AUS")
head(Champ)
names(Champ) <- c("HomeTeam", "AwayTeam", "FTHG", "FTAG")
head(Fix)
Head(Champ)
Head(Fix)
该代码的第1部分从URL中的任何联赛中抓取结果(冠军)和赛程(修复)。
代码第2部分。
library("vcd")
source("http://www.maths.leeds.ac.uk/~voss/projects/2010-sports/Football.R")
results0 <- read.csv("AUS",stringsAsFactors = F)
results0$X <- NULL
results0$X.1 <- NULL
results0$NA. <- NULL
results0
names(results0)<- c("HomeTeam", "AwayTeam", "FTHG", "FTAG")
head(results0)
Parameters(results0)
countres <- results0$FTHG + results0$FTAG
table(countres)
tg <- countres
fretabtg<-table(tg)
fretabtg
gf <- goodfit(fretabtg, type="poisson", method="ML")
gf
summary(gf)
Table0 <- Table(results0)
Table0
mean(results0[,3])
mean(results0[,4])
var(results0[,3])
var(results0[,4])
games <- results0
g <- nrow(games)
Y <- matrix(0,2*g,1)
for (i in 1:g) {
Y[((2*i)-1)] <- games[i,3]
Y[(2*i)] <- games[i,4]
}
teams <- sort(unique(c(games[,1], games[,2])), decreasing = FALSE)
n <- length(teams)
X <- matrix(0,2*g,((2*n)+1))
for (i in 1:g) {
M <- which(teams == games[i,1])
N <- which(teams == games[i,2])
X[((2*i)-1),M] <- 1
X[((2*i)-1),N+n] <- -1
X[(2*i),N] <- 1
X[(2*i),M+n] <- -1
X[((2*i)-1),((2*n)+1)] <- 1
}
x <- qr(X)
x$rank
XX <- X[,-1]
glm(formula = Y ~ 0 + XX, family = poisson)
TeamParameters <- Parameters(results0)
TeamParameters
SimSeason <- Games(TeamParameters)
SimSeason
SimTable <- Table(SimSeason)
SimTable
Simulations <- Sim(TeamParameters,3)
Simulations
Probabilities <- ProbTable(TeamParameters,"Augsburg","Freiburg")
ResultProbabilities<- ResultProbs(Probabilities)
ResultProbabilities
cat("\nHome Win:", 100/ResultProbabilities$HomeWin)
cat("\nDraw:", 100/ResultProbabilities$Draw)
cat("\nAway Win:", 100/ResultProbabilities$AwayWin)
代码的第2部分用于确定概率。
答案 0 :(得分:1)
请考虑Map
在Fix$X2
和Fix$X4
团队之间进行元素迭代。下面显示了cat
调用,并将每个 ResultProbabilities 保存到列表中:
run_probs <- function(h_team, a_team) {
Probabilities <- ProbTable(TeamParameters, h_team, a_team)
ResultProbabilities <- ResultProbs(Probabilities)
cat(paste("\n", h_team, "VS", a_team))
cat("\nHome Win:", 100/ResultProbabilities$HomeWin)
cat("\nDraw:", 100/ResultProbabilities$Draw)
cat("\nAway Win:", 100/ResultProbabilities$AwayWin)
return(ResultProbabilities)
}
prob_list <- Map(run_probs, Fix$X2, Fix$X4)