Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在R中为此做一个循环/函数吗?(我该怎么做?)_R_Function_Loops_Csv - Fatal编程技术网

我可以在R中为此做一个循环/函数吗?(我该怎么做?)

我可以在R中为此做一个循环/函数吗?(我该怎么做?),r,function,loops,csv,R,Function,Loops,Csv,我目前正在使用R来回溯测试一些足球/足球赔率,使用一个模型来创建我自己的赔率 目前,这是一个相当长的过程,我很好奇,好像有一个循环/函数可以帮助我加速这个过程 这段代码收集整个赛季的结果 library(dplyr) library(rvest) library(tidyverse) options(max.print = 9999) Res <- read_html("https://www.betexplorer.com/soccer/england/premier-league/re

我目前正在使用R来回溯测试一些足球/足球赔率,使用一个模型来创建我自己的赔率

目前,这是一个相当长的过程,我很好奇,好像有一个循环/函数可以帮助我加速这个过程

这段代码收集整个赛季的结果

library(dplyr)
library(rvest)
library(tidyverse)
options(max.print = 9999)
Res <- read_html("https://www.betexplorer.com/soccer/england/premier-league/results/?month=all")
tbls_ls <- Res %>%
  html_nodes("table") %>%
  .[1] %>%
  html_table(fill = TRUE)

Results <- as.data.frame(tbls_ls)
Results <- Results[,c(1:2)]
names(Results) <- c("Fixture","Score")
Results <- tidyr::separate(Results, Fixture, into =c("HomeTeam","AwayTeam"), sep = " - ")
Results <- tidyr::separate(Results, Score, into = c("FTHG","FTAG"), sep = ":")
Results <- Results %>% tidyr::drop_na()
Results <- Results[,c(1:4)]
write.csv(Results, file = "Results.csv")
rownames(Results) <- 1:nrow(Results)
等等

希望每周的比赛都能有这样的回报

             Home            Away   Home Win      Draw  Away Win
1       Leicester     Aston Villa   1.209044  9.009009  16.18123
2         Chelsea         Everton   1.634788   5.09165  5.216484
3  Manchester Utd Manchester City      3.125  4.199916  2.265006
4         Arsenal        West Ham   1.786352   4.52284   4.56621
5         Burnley       Tottenham    3.08642  3.904725  2.379819
6  Crystal Palace         Watford   2.309469  3.079766  4.128819
7       Liverpool     Bournemouth   1.160362  10.04016  25.97403
8   Sheffield Utd         Norwich   1.637465  3.868472  7.639419
9     Southampton       Newcastle   2.198769  3.687316  3.654971
10         Wolves        Brighton   1.785714  4.016064  5.230126

很抱歉发了这么长的帖子,如果我没什么道理,我也很抱歉。如果这看起来像胡言乱语,请随意锁定/移除帖子

似乎有很多代码没有在您试图完成的任务中使用。 您似乎还对一些顺序不正确的游戏有问题,这可能是有问题的

以下是我对如何更有效地运行此功能的看法-如果我正确理解了您的意图:

库(dplyr)
图书馆(rvest)
图书馆(tidyverse)
库(数据表)
选项(最大打印数=9999)
Res%
.[1] %>%
html_表(fill=TRUE)

结果球探杰伊,请花点时间,试着尊重他的“M”。这里有很多代码完全不起任何作用(对我们来说),没有持久的影响(只会产生副作用或内省),只是让你的问题变得模糊。例如:对
glm
mean
var
,以及用于查看某些内容的任何代码行的未保存调用,但由于此处未显示,我只能推断我们不需要查看该代码行。长的问题可能是一种威慑,考虑缩短到最少的重复代码来演示这个问题。我只是略过了你的代码,但是既然你已经使用了<代码> TydErrase<代码>,那么你可以通过使用管道( %%%%/Cord>)操作符,使它更短和更可读。这是很棒的工作-非常感谢。这正是我想要的。但我认为可能有一个可能性的问题。回到我一直手工操作的方式。我在利物浦对曼联的比赛中(在24周内),我的主场战平率为1.58,客场战平率为4.55,客场战平率为6.75。当我运行你提供的代码时,它给了我一个答案:1.39抽签:5.57抽签:10.17。。。。你认为这是造成差异的原因吗?看看原始表,你可能需要考虑如何处理属于不同回合的数据,或者丢失的数据(在这个时间跨度内每回合少于2次的游戏少于10次)。我把所有的回合都放在一起,然后陆续地把它们移走。在第24轮之后的第18轮比赛中,有一场比赛被推迟了,那就是西汉姆-利物浦。如果您希望保持顺序,可以按日期排序或手动指定周。
ResultsEdit <- Results [-(1:10),]
ResultsEditE <- Results [-(1:20),]
library("vcd")

source("http://www.maths.leeds.ac.uk/~voss/projects/2010-sports/Football.R")
results0 <- read.csv("ResultsEditE",stringsAsFactors = F) 
results0$X <- NULL
countres <- results0$FTHG + results0$FTAG
tg <- countres
fretabtg<-table(tg)
gf <- goodfit(fretabtg, type="poisson", method="ML")
Table0 <- Table(results0)
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]



TeamParameters <- Parameters(results0)
SimSeason <- Games(TeamParameters)
SimSeason <- SimSeason %>% tidyr::drop_na()

SimTable <- Table(SimSeason)
Simulations <- Sim(TeamParameters,3)

Probabilities <- ProbTable(TeamParameters,"", "")
ResultProbabilities<- ResultProbs(Probabilities)

cat("\nHome Win True Odds:", 100/ResultProbabilities$HomeWin)
cat("\nDraw True Odds:", 100/ResultProbabilities$Draw)
cat("\nAway Win True Odds:", 100/ResultProbabilities$AwayWin)

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)
}

FixEdit <- head(FixEdit, n=10)

prob_list <- Map(run_probs, FixEdit$HomeTeam,FixEdit$AwayTeam)
Run the game week 29 removal code, run the poisson code, run the code for giving me the odds for the game week - save the results in a CSV
Run the game week 28 removal code, run the poisson code, run the code for giving me the odds for the game week - save the results in a CSV
             Home            Away   Home Win      Draw  Away Win
1       Leicester     Aston Villa   1.209044  9.009009  16.18123
2         Chelsea         Everton   1.634788   5.09165  5.216484
3  Manchester Utd Manchester City      3.125  4.199916  2.265006
4         Arsenal        West Ham   1.786352   4.52284   4.56621
5         Burnley       Tottenham    3.08642  3.904725  2.379819
6  Crystal Palace         Watford   2.309469  3.079766  4.128819
7       Liverpool     Bournemouth   1.160362  10.04016  25.97403
8   Sheffield Utd         Norwich   1.637465  3.868472  7.639419
9     Southampton       Newcastle   2.198769  3.687316  3.654971
10         Wolves        Brighton   1.785714  4.016064  5.230126