Trying to create a 'for' loop with a poisson model inside produces - "error: variable 'team' was fitted with type "factor" but type "numeric" was supplied"

Hi, this is my first post here, any help would be hugely appreciated.
I'm not too familiar with loops and I'm attempting to make a very complicated one on premier league data with a poisson model contained in the loop. I'm basically trying to use the results of the poisson model for the goals scored for each team and to turn these scores into actual results (Home win, Away win or Draw).
The code is as follows:

for (i in 1:320) {
poissonclassifier[i] = {
  home[i] = predict(poissonmodel, 
        data.frame(home=1, team = prem[i,2], 
                  opponent =prem[i,3]), type="response")
  away[i] = predict(poissonmodel, 
                    data.frame(home=0, team=prem[i,1], 
                               opponent=prem[i,2]), type="response")
game[i] = dpois(0:5, home[i]) %o% dpois(0:5, away[i])
rownames(game[i]) =  0:5
colnames(game[i]) = 0:5
H = sum(game[i][lower.tri(game[i])])
D = sum(game[i][diag(game[i])])
A = sum(game[i][upper.tri(game[i])])
result[i] = max("H","D","A")
result[i]}
}

where

poissonmodel = glm(goals ~ home + team + opponent, family=poisson(link=log),data=prem)

home[i] and away[i] predict the number of goals for the home and away teams respectively, and 'prem' is a table of the games this season, with 'home' as 1 to indicate the home team, and 0 to indicate the away team. (For example in the first row below, liverpool is the home team and there is a 1 in the home column, so the total goals for liverpool in this game was 4). Below is the first 10 rows of this dataset:

goals             team         opponent home
1       4        Liverpool          Norwich    1
2       0         West Ham         Man City    1
3       1      Bournemouth Sheffield United    1
4       3          Burnley      Southampton    1
5       0   Crystal Palace          Everton    1
6       0          Watford         Brighton    1
7       3        Tottenham      Aston Villa    1
8       0        Leicester           Wolves    1
9       0        Newcastle          Arsenal    1
10      4       Man United          Chelsea    1

I'm mainly confused as to why I get the error message above, as I need to keep "team" as a factor variable not a numeric variable so I know which team has played which.
I appreciate that this is a lot of information at once and there may not be one single answer, so any help would be greatly appreciated. If you spot any other mistakes with my code don't hesitate to say too!!

Thanks :slight_smile:

I do not really understand your code but in the part quoted above the team argument is set to prem[i, 1] which is the goals column and has a numeric value. Should it be prem[i, 3]?

1 Like

Ah thank you well spotted!!! However upon changing that I now get the error message "Error: object 'game' not found", but surely I need to define it in the environment?

I've updated the post by the way, hopefully the code now makes more sense.

I am not sure what you mean by the above phrase. Yes, game should be defined before the for loop. It seems you know how many elements it will have so you can use the vector function to define it.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.