Dealing with recursion depth limitation in R - r

The algorithm is from https://www.math.upenn.edu/~wilf/eastwest.pdf page 16 RandomKSubsets
RandomKSubsets = function(n, k){
if (n<0 | k<0 | k<n){
return()
}
else {
if (n==0 && k==0){
return(c())
}
else {
rno = runif(1)
if (rno < n/k){
east = RandomKSubsets(n-1,k-1)
return (c(east, k))
}
else{
west = RandomKSubsets(n,k-1)
return(west)
}
}
}
}
Running the program with k=4000 and n=1200 I run into recursion depth limit. I tried options(expressions=500000) but it's not enough for the algorithm. How can I run this code for my variables?

This is close to tail recursion: the only recursive calls are in the return statements. This blog: http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html describes how to change such functions into loops. I followed the mostly mechanical process described there, and came up with this version:
RandomKSubsetsLoop = function(n, k) {
acc <- NULL
while (TRUE) {
if (n<0 | k<0 | k<n){
return(acc)
}
else {
if (n==0 && k==0){
return(acc)
}
else {
rno = runif(1)
if (rno < n/k){
acc <- c(k, acc)
k <- k - 1
n <- n - 1
next
}
else{
k <- k - 1
next
}
}
}
break
}
}
I haven't tested it extensively, but it produces the same result as the original in this test:
set.seed(1)
RandomKSubsets(5, 10)
# [1] 1 3 6 9 10
set.seed(1)
RandomKSubsetsLoop(5, 10)
# [1] 1 3 6 9 10
You'll probably want to do more extensive testing, and read the blog to make sure I've done things as it describes.
By the way, there are other algorithms to do this sampling, e.g. the one described in
AUTHOR="McLeod, A.I. and Bellhouse, D.R. ",
YEAR = 1983,
TITLE="A convenient algorithm for drawing a simple random sample",
JOURNAL="Applied Statistics",
VOLUME="32",
PAGES="182-184"
That one is based on a loop by design, and has the advantage that you don't need to know the population size (k in your notation) in advance: you just keep updating your sample until there are no more items to process.

Related

How to implement a function with a sum inside in R?

I am trying to define a function with a for loop and inside a conditional in R studio. Yesterday I was able with the help of another thread to devise this piece of code. The problem is that I want to sum the vector elements ma for any possible x, so that is inside the function l. This is a simpler case which I am trying to solve to adapt the original model. However, I do not know how to proceed.
ma<-rep(0,20)
l <- function(x, ma) {
for(i in seq_along(ma)) {
if(i %% 2 == 1) {
ma[i] <- i + x
} else {
ma[i] <- 0
}
}
return(ma)
}
My problem is that I would like to have the sum of i+x+0+i+x... for any possible x. I mean a function of the kind for any possible x.
Question:
Can someone explain to me how to implement such a function in R?
Thanks in advance!
I am going to update the original function:
Theta_alpha_s<-function(s,alpha,t,Basis){
for (i in seq_along(Basis)){
if(i%% 2==1) {Basis[i]=s*i^{-alpha-0.5}*sqrt(2)*cos(2*pi*i*t)}
else{Basis[i]=s*i^{-alpha-0.5}*sqrt(2)*sin(2*pi*i*t)}
}
return(Basis)
}
If you don't want to change the values in Basis, you can create a new vector in the function (here result) that you will return:
l = function(s,alpha,t,Basis){
is.odd = which(Basis %% 2 == 1)
not.odd = which(Basis %% 2 == 0)
result = rep(NA, length(Basis))
result[is.odd] = s*is.odd^{-alpha-0.5}*sqrt(2)*cos(2*pi*is.odd*t)
result[not.odd] = s*not.odd^{-alpha-0.5}*sqrt(2)*sin(2*pi*not.odd*t)
#return(result)
return(c(sum(result[is.odd]), sum(result[not.odd])))
}

confunsion in create if in R

I have a question how to make a IF
for (i in 1:12){
for (j in 1:12) {
if (i != j) {
var = x + b
}
else{ }
}}
"else" I need that when they are equal to continue with j + 1 example: if i = 4 and j = 4 then continue with j = 5 and continue counting until the end of j and continue the process of when i! = j
I think you don't understand what is going on in your code or you don't understand what for loops do. One "trick" you can do is to actually print what happens in your for loops so that you will have one idea of what is going on. You could also do this with a piece of paper.
As they already pointed you out, you don't need the else because the for already takes care of this.
for (i in 1:12){
print("-------------------------------")
valueI <- paste0("my i value is ",i)
print(valueI)
for (j in 1:12) {
valueJ <- paste0("my j value is ",j)
print(valueJ)
if (i != j) {
#var = x + b
diff <- paste0(i, " is different than ", j)
print(diff)
}
else{
}
}
}
This code is the same as yours and will generate a log that explains you what happens step from step, you could also use a debugger but seeing your struggles, better use this for now. What are you trying to calculate? I feel like you want to calculate the power of something...

Practicing loops

I'm trying to improve my function writing skills and I'm a little confused on the proper structure of functions. I've searched a ton of examples, but none are that clear to me. My aim is to run the #RUN over and over section in a for loop and build a function which allows me to control the number of times I can loop it.
Currently, I've got to this point:
set.seed(123)
#Start but setting the conditions and being the Win Lose counters
Count_Win_Hunt=0
Count_Win_Moose=0
#RUN over and over
Hunter=1
Moose=7
win=0
while(win != 1){ a = sample(1:6, 1) # dice roll
if( a<= 4) {Moose = Moose+a} else{Hunter = Hunter+a}
if( Hunter >= Moose ) { Count_Win_Hunt = Count_Win_Hunt +1 } else if( Moose >= 12) {Count_Win_Moose = Count_Win_Moose + 1}
if( Hunter >= Moose || Moose >= 12 ) {win = win+1} else {
#if not condition not meet roll again
a = sample(1:6, 1) # dice roll
if( a<= 4) {Moose = Moose+a} else{ Hunter = Hunter+a}}}
# calculated the average win rates
paste0( round(Count_Win_Hunt/(Count_Win_Hunt+Count_Win_Moose),4)*100,"%"," of the time the Hunter won")
paste0( round(Count_Win_Moose/(Count_Win_Hunt+Count_Win_Moose),4)*100,"%"," of the time the Moose won")
Besides my general problems with your question (please be more specific as to your actual problem) your for-loops have a wrong syntax. They should be like this:
for (val in sequence)
{
statement
}
So applied to your function they should look like this:
for (val in c(1:4))
{
probability + (hunter,goose+val,num+1)
}
for (val in c(5:6))
{
probability + (hunter,goose+val,num+1)
print probability
}
However the are not only syntactically wrong, also their content seems to be wrong.
E.g. in your second for-loop, the goose steps forward even though it should be the hunter. Also these are not two for-loops but should be an if-statement like this:
if (val <= 4) {
probability + (hunter,goose+val,num+1)
}
else {
probability + (hunter+val,goose,num+1)
}
Finally the whole structure of your function seems strange (and has misleadingly named variables). Shouldn`t it be something like this:
dice_roll <- function(hunter,goose, win){
# While to check for winning condition
while(win != 1){
dice_roll = sample(1:6, 1) # simulate dice roll
# If statement depending on dice roll, increasing value of hunter or goose by dice roll
# Change win condition
If(hunter >= goose){
win <- 1
}
}
dice_roll(1,7,0)

Why does a readLines (from url) result change after some iterations (in R)?

My problem is that after some iterations in R the readLines() function doesn't extract the information needed anymore. And I don't know where this problem comes from.
I would like to scrape some player statistics from www.whoscored.com and loop over these players --> https://www.whoscored.com/Players/i
for (i in 1:20){
sc_act <- readLines("https://www.whoscored.com/Players/101537", warn = FALSE)
if (i == 1){
sc <- sc_act
j <- 0
}
if (sc == sc_act){
j <- j + 1
}
}
On the first iterations the result comes out as expected, it reads the sourcecode of the mentioned url into sc_act.
But then (after about 10 iterations)the result looks like this:
"<html style=\"height:100%\"><head><META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\"><meta name=\"format-detection\" content=\"telephone=no\"><meta name=\"viewport\" content=\"initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><script type=\"text/javascript\" src=\"/_Incapsula_Resource?SWJIYLWA=719d34d31c8e3a6e6fffd425f7e032f3\"></script></head><body style=\"margin:0px;height:100%\"><iframe src=\"/_Incapsula_Resource?SWUDNSAI=28&xinfo=9-5358627-0%200NNN%20RT%281545484419406%2074%29%20q%280%20-1%20-1%20-1%29%20r%280%20-1%29%20B12%2811%2c55645%2c0%29%20U2&incident_id=287001440012879521-35322777428756745&edet=12&cinfo=0b000000\" frameborder=0 width=\"100%\" height=\"100%\" marginheight=\"0px\" marginwidth=\"0px\">Request unsuccessful. Incapsula incident ID: 287001440012879521-35322777428756745</iframe></body></html>"
You are calling too much times quickly the same url, I recommed you to read the book "Automated Data Collection with R". One simple way to fix your issue could be to waint some seconds between iterations.
for (i in 1:20){
sc_act <- readLines("https://www.whoscored.com/Players/101537", warn = FALSE)
if (i == 1){
sc <- sc_act
j <- 0
}
if (sc == sc_act){
j <- j + 1
}
time <- runif(n = 1, 3, 5)
Sys.sleep(time) # Wait between 3 and 5 seconds each iteration
}
Or maybe changing your user agent...

R S3 cat() output from function

I've got a problem with output from S3 function. I try to overload "+" function to act with two vectors like with polynomial parameters. It's my university project. Code is below:
'+.ply' <- function(a,b){
size <- max(length(a$polynomial),length(b$polynomial))
size
aAdd <- a$polynomial
bAdd <- b$polynomial
if (length(aAdd) == size) {
aAdd = aAdd
} else {
length(aAdd) <- size
}
aAdd[is.na(aAdd)] <- 0
if (length(bAdd) == size) {
bAdd = bAdd
} else {
length(bAdd) <- size
}
bAdd[is.na(bAdd)] <- 0
cat("Polynomial of degree ", paste(length(aAdd+bAdd)-1),
" with coefficients ", paste(aAdd+bAdd))
}
Code is working fine, but in return it gives me output
*Polynomial of degree 3 with coefficients 3 4 6 3NULL*
I need to use cat in order to avoid [1] index which occurs while I'm using print, paste combo. I know that there are plenty threads about this problem, but I can't find any sollution for such problem during function overloading. I will be thankful for help.

Resources