Hello!
Is there a way to plot a 2D reinforced random walk with different values for alpha?
Next_step = function(i,j,Grille){
l = nrow(Grille)
prob = rep(NA,4)
prob[1] = Grille[i+1,j]/(Grille[i,j-1]+Grille[i,j+1]+Grille[i-1,j]+Grille[i+1,j])
prob[2] = Grille[i,j+1]/(Grille[i,j-1]+Grille[i,j+1]+Grille[i-1,j]+Grille[i+1,j])
prob[3] = Grille[i,j-1]/(Grille[i,j-1]+Grille[i,j+1]+Grille[i-1,j]+Grille[i+1,j])
prob[4] = Grille[i-1,j]/(Grille[i,j-1]+Grille[i,j+1]+Grille[i-1,j]+Grille[i+1,j])
next_step = sample(1:4, 1,replace = TRUE, prob = prob)
if (next_step == 1) { # en bas
i = i+1
} else if (next_step == 2) { # à droite
j = j+1
} else if (next_step == 3) { # à gauche
j= j-1
} else if (next_step == 4) { # en haut
i = i-1
}
return(c(i,j))
}
RW_2d_start = function(n,pas, alpha){
Grille = matrix(1, nrow = n+2, ncol = n+2)
Grille[c(1,n+2),] = 0
Grille[,c(1,n+2)]=0
axe_x = rep(NA,pas)
axe_y = rep(NA,pas)
d = rep(NA,2)
if (n%%2 != 0) {axe_x[1] = floor(n/2)} else {axe_x[1] = n/2} ## on commence du milieu
axe_y[1] = axe_x[1]
Grille[axe_x[1],axe_y[1]] = Grille[axe_x[1],axe_y[1]]+alpha
for (i in 2:pas){
d[1:2]= Next_step(axe_x[i-1],axe_y[i-1],Grille)
axe_x[i]=d[1]
axe_y[i]=d[2]
Grille[d[1],d[2]] = Grille[d[1],d[2]]+alpha
}
return(list(path = data.frame(step = seq_along(axe_x), x = axe_x, y = axe_y),
Grille = Grille))
}
r <- RW_2d_start(100,10000,0)
r_path <- r$path
ggplot(r_path) +
theme_classic() +
geom_path(aes(x = x, y = y, color = step)) +
scale_color_steps(low = "blue",
high = "red") +
labs(
title = "alpha = 0",
x = "position x",
y = "position y")
I want to plot RW_2d_start function with different values of alpha and the number of steps in the same graph.
Is this possible?
Thank you!