Applying technical trading rule to all combination of inputs - r

I have a Dataframe with columns containing different time series of data. I need to insert these columns into the function below automatically to find the best combination (highest return):
returns <- as.data.frame(rep(0, 4261)) #4261 because that's the length of n1.lc
returns$n2_5_10 <- rep(0, nrow(returns))
MSVrule <- function(n1, n2, hold){
for(i in 13:nrow(n1.lc)){
if (n1[i] > n2[i] & n1[i-1] < n2[i-1]) {
returns$n2_5_10[i] <- dt.lc$Settle[i - hold] - dt.lc$Settle[i]
} else {
if (n1[i] < n2[i] & n1[i-1] > n2[i-1])
{returns$n2_5_10[i] <- dt.lc$Settle[i] - dt.lc$Settle[i - hold]
}
else{
NULL
}
}
}
}
MSVrule(n1.lc$N1_2_5, n2.lc$n2_2_10, 5)
This function doesn't work, leaving returns$n2_5_10[i] 0 However, when I specifiy the vectors in the function it works:
hold <- 5
for(i in 13:nrow(n1.lc)){
if (n1.lc$N1_2_5[i] > n2.lc$n2_2_10[i] & n1.lc$N1_2_5[i-1] < n2.lc$n2_2_10[i-1]) {
returns$n2_5_10[i] <- (dt.lc$Settle[i - hold] - dt.lc$Settle[i]) / dt.lc$Settle[i]
} else {
if (n1.lc$N1_2_5[i] < n2.lc$n2_2_10[i] & n1.lc$N1_2_5[i-1] > n2.lc$n2_2_10[i-1])
{returns$n2_5_10[i] <- (dt.lc$Settle[i] - dt.lc$Settle[i - hold]) / dt.lc$Settle[i - hold]
}
else{
NULL
}
}
}
The next step would be to automatically apply the function to other combinations of vectors from the n1.lc Dataframe. But I need the function to work first.

Because you use a function with no return() attempting to modify objects outside its scope, nothing external will be changed. However, you can use <<- operator to modify external objects to the function, namely the n2_5_10 column in returns:
Below demonstrates with random generated data and assigns the column in returns with 500 or 5 as you do not post other needed, used objects (i.e., dt.lc). Adjust to actual data objects:
n1.lc <- data.frame(
N1_2_5 = runif(50),
n2_2_10 = runif(50)
)
returns <- data.frame(n2_5_10 = rep(0, nrow(n1.lc)))
MSVrule <- function(n1, n2, hold){
for(i in 13:nrow(n1.lc)){
if (n1[i] > n2[i] & n1[i-1] < n2[i-1]) {
returns$n2_5_10[i] <<- 500
} else {
if (n1[i] < n2[i] & n1[i-1] > n2[i-1])
{returns$n2_5_10[i] <<- 5
}
else{
NULL
}
}
}
}
MSVrule(n1.lc$N1_2_5, n1.lc$n2_2_10, 5)
However, consider using return where you create/update/output returns inside function. Then on outside, assign a new dataframe as the function's output (no need for else { NULL } clause):
MSVrule <- function(n1, n2, hold){
returns <- data.frame(n2_5_10 = rep(0, nrow(n1.lc)))
for(i in 13:nrow(n1.lc)){
if (n1[i] > n2[i] & n1[i-1] < n2[i-1]) {
returns$n2_5_10[i] <- 500
} else {
if (n1[i] < n2[i] & n1[i-1] > n2[i-1])
returns$n2_5_10[i] <- 5
}
}
return(returns)
}
newdf <- MSVrule(n1.lc$N1_2_5, n1.lc$n2_2_10, 5)
# BOTH ABOVE RESULT IN EQUIVALENT OUTCOMES
all.equal(returns, newdf)
# TRUE
identical(returns, newdf)
# TRUE

Related

R user-defined function called but not running

I am new to R, and I am trying to create a user-defined function and run it inside a for loop. The function is defined in the same script before it is called. However, when the function is called, the code inside the function doesn't run. No syntax errors occur.
Here is the function definition:
placeInVector <- function(subjectID, morpho) {
if (morpho == "Alg") {
algVec <- c(algVec, subjectID)
}
else if (morpho == "Anem") {
anemVec <- c(anemVec, subjectID)
}
else if (morpho == "Barn") {
barnVec <- c(barnVec, subjectID)
}
else if (morpho == "Biv") {
bivVec <- c(bivVec, subjectID)
}
else if (morpho == "BrBryo") {
brbryoVec <- c(brbryoVec, subjectID)
}
else if (morpho == "ColTuni") {
coltuniVec <- c(coltuniVec, subjectID)
}
else if (morpho == "EnBryo") {
enbryoVec <- c(enbryoVec, subjectID)
}
else if (morpho == "Nothing") {
nothingVec <- c(nothingVec, subjectID)
}
else if (morpho == "SolTuni") {
soltuniVec <- c(soltuniVec, subjectID)
}
else if (morpho == "Spng") {
spngVec <- c(spngVec, subjectID)
}
else if (morpho == "TubeWm") {
tubewmVec <- c(tubewmVec, subjectID)
}
}
Here is where the function is called later on in the script:
for (subject in subjects$SubjectID) {
currentRow <- finalIDs %>%
filter(SubjectID == subject)
morphoID <- as.character(currentRow$FinalIDs1)
if (morphoID != "NONE") {
placeInVector(subject, morphoID)
}
}
Note: all of the vectors referenced in the function definition (algVec, anemVec, etc.) are defined as empty vectors.
As far as I understand, you are trying to add elements to vectors. Because I don't have a reproducible example, I will use my own. Here, add_to_vector adds elements to a and b:
a <- c(1,2)
b <- c("a","b")
add_to_vector <- function(vec, to_add){
vec <- c(vec, to_add)
return(vec)
}
a <- add_to_vector(a, 3)
b <- add_to_vector(b, "c")
Hopefully, this gives you an idea of how R works.
Output
> a
[1] 1 2 3
> b
[1] "a" "b" "c"

implement matrix determinant in R

I was asked to implement function that calculates n-dimensional matrix determinant using Laplace expansion. This involves recursion. I developed this:
minor<-function(A,i,j) {
return(A[c(1:(i-1),(i+1):dim(A)[1]),c(1:(j-1),(j+1):dim(A)[2])])
}
determinantRec<-function(X,k) {
if (dim(X)[1] == 1 && dim(X)[2] == 1) return(X[1][1])
else {
s = 0
for (i in 1:dim(X)[2]) {
s = s + X[k][i]*(-1)^(k+i)*determinantRec(minor(X,k,i),k)
}
return(s)
}
}
where k in determinantRec(X,k) function indicates which row I want to use Laplace expansion along of.
My problem is when I run determinantRec(matrix(c(1,2,3,4),nrow = 2,ncol = 2),1) this error appears:
C stack usage 7970628 is too close to the limit
What is wrong with my code?
#julia, there is one simple type in your code. Just remove the '*' at the end of the definition of 's'. And don't indent the recursion.
determinantRek<-function(X,k) {
if (dim(X)[1] == 1 && dim(X)[2] == 1)
return(X[1,1])
if (dim(X)[1] == 2 && dim(X)[2] == 2)
return(X[1,1]*X[2,2]-X[1,2]*X[2,1])
else
s = 0
for (i in 1:dim(X)[2]) {
s = s + X[k,i]*(-1)^(k+i)
determinantRek(X[-k,-i],k)
}
return(s)
}
I did this way and works just fine, although it is super slow, compared to the det function in base R
laplace_expansion <- function(mat){
det1 <- function(mat){
mat[1]*mat[4]-mat[2]*mat[3]
}
determinant <- 0
for(j in 1:ncol(mat)){
mat1 <- mat[-1,-j]
if(nrow(mat1) == 2){
determinant <- determinant+mat[1,j]*(-1)^(1+j)*det1(mat1)
}else{
val <- mat[1,j]*(-1)^(1+j)
if(val != 0){
determinant <- determinant+val*laplace_expansion(mat1)
}
}
}
return(determinant)
}
This is my approach, I think it's cleaner.
deter <- function(X) {
stopifnot(is.matrix(X))
stopifnot(identical(ncol(X), nrow(X)))
if (all(dim(X) == c(1, 1))) return(as.numeric(X))
i <- 1:nrow(X)
out <- purrr::map_dbl(i, function(i){
X[i, 1] * (-1)^(i + 1) * deter(X[-i, -1, drop = FALSE])
})
return(sum(out))
}
Thank you #ArtemSokolov and #MrFlick for pointing the problem cause, it was it. I also discovered that this code does not calculate properly the determinant of 2x2 matrix. After all it looks like that:
determinantRek<-function(X,k) {
if (dim(X)[1] == 1 && dim(X)[2] == 1)
return(X[1,1])
if (dim(X)[1] == 2 && dim(X)[2] == 2)
return(X[1,1]*X[2,2]-X[1,2]*X[2,1])
else
s = 0
for (i in 1:dim(X)[2]) {
s = s + X[k,i]*(-1)^(k+i)*
determinantRek(X[-k,-i],k)
}
return(s)
}
Debuging with browser() was also helpful :)

Why is break() not working in this loop? (but stop is)

I am trying to build a matrix model which ends if certain conditions are invoked - however for some reason the break() command isn't working, although stop() does. Unfortunately stop() is not what I need as I need to run the model a number of times.
The first break command in the model works, but I have left it in with dth>100 so that you can see for yourselves
n.steps <- 200
ns <- array(0,c(14,n.steps))
ns[13,1]<-rpois(1,3)
ns[14,1] <- 1
k<-0
for (i in 1:n.steps){
k<-k+1
ns[13,1]<-rpois(1,2)
death<-sample(c(replicate(1000,
sample(c(1,0), prob=c(surv.age.a, 1-surv.age.a), size = 1))),1)
ns[14,k] <- death
if (death == 0) {
dth <- sample(1:100, 1)
if (dth > 100) {
ns[14,k]<-0
print("stop.1")
break()
} else {
while (death == 0) {
if (ns[13, k] > 0) {
rep.vec[i]<-ns[13,k]
ns[13, k] <- ns[13, k] - 1
ns[14,k+1]<-1
print("replace")
} else {
if (ns[13, k] == 0) {
print("stop.2")
ns[14,k+1]<-0
break()
}
}
}
}
}
}
Try this (only showing the relevant portions):
for (i in 1:n.steps){
# ...
break.out.of.for <- FALSE
while (death == 0) {
if (ns[13, k-1] > 0) {
# ...
} else {
if (ns[13, k] == 0) {
# ...
break.out.of.for = TRUE
break
}
}
if (break.out.of.for) {
break
}
}

KnapSack dynamic programming in R with recursive function

I created this simple code in R to solve the Knapsack program with a recursive funtion
n <- c(0,1,2,3,4)
v <- c(10,40,30,50)
w <- c(5,4,6,3)
k <- 10
myfunction <- function(n,k){
if (n==0 | k==0){
output <- 0
} else if (w[i] > k) {
output <- myfunction[i-1,w]
} else {
output <- max(v[i]+ myfunction(i-1, k-w[i]),myfunction(i-1,k))
}
return(myfunction)
}
However, I don't get a value as an output, but the whole function. For example if I put in:
myfunction(4,10)
I don't get an value of 90, but the whole funtion typed out.
these are the values
There were several errors beyond the ones pointed out by #etienne. Here's an annotated debugging session. First we fix the returned object:
> myfunction <- function(n,k){
+ if (n==0 | k==0){
+ output <- 0
+ } else if (w[i] > k) {
+ output <- myfunction[i-1,w]
+ } else {
+ output <- max(v[i]+ myfunction(i-1, k-w[i]),myfunction(i-1,k))
+ }
+ return(output)
+ }
> myfunction(4,10)
Error in if (w[i] > k) { : argument is of length zero
Obviously neither w nor k are of length zero which suggests it must be i. (As also pointed out by etienne). Looking at your code it appears you actually intended i to be the index that decreased until the terminating condition was met. So replace n by i in the few instances where it appeared:
> myfunction <- function(i,k){
+ if (i==0 | k==0){
+ output <- 0
+ } else if (w[i] > k) {
+ output <- myfunction[i-1,w]
+ } else {
+ output <- max(v[i]+ myfunction(i-1, k-w[i]),myfunction(i-1,k))
+ }
+ return(output)
+ }
> myfunction(4,10)
Error in myfunction[i - 1, w] :
object of type 'closure' is not subsettable
So you also made the mistake of using square-brackets where parentheses (aka bracket in the non-US sections of the world) were needed:
> myfunction <- function(i,k){
+ if (i==0 | k==0){
+ output <- 0
+ } else if (w[i] > k) {
+ output <- myfunction(i-1,w)
+ } else {
+ output <- max(v[i]+ myfunction(i-1, k-w[i]),myfunction(i-1,k))
+ }
+ return(output)
+ }
> myfunction(4,10)
[1] 90
Success, well, almost. Most of the warnings are because you used | instead of || in one of the conditionals:
Warning messages:
1: In if (i == 0 | k == 0) { :
the condition has length > 1 and only the first element will be used
2: In if (w[i] > k) { :
the condition has length > 1 and only the first element will be used
3: In if (i == 0 | k == 0) { :
the condition has length > 1 and only the first element will be used
4: In if (i == 0 | k == 0) { :
the condition has length > 1 and only the first element will be used
5: In if (i == 0 | k == 0) { :
the condition has length > 1 and only the first element will be used
6: In if (i == 0 | k == 0) { :
the condition has length > 1 and only the first element will be used
So replace that instance with a logical ||. To deal with the other warning that didn't seem to sabotage your logic, realize that w[i] is length-0 when i == 0, so add a logical clause in the conditional that first tests for that possibility and use the correct "double-AND-sign" ( && ):
myfunction <- function(i,k){
if (i==0 || k==0){
output <- 0
} else if (length( w[i]) && w[i] > k) {
output <- myfunction(i-1,w)
} else {
output <- max(v[i]+ myfunction(i-1, k-w[i]), myfunction(i-1,k))
}
return(output)
}
Now you get:
> myfunction(4,10)
[1] 90

sapply in r with user defined function

My code is as follows:
data("USArrests")
AssignLevel <- function(p,quartiles)
{
if (p < quartiles[1])
rlevel <-"LOW"
else if (p < quartiles[2])
rlevel <-"MODERATE"
else if (p < quartiles[3])
rlevel <-"HIGH"
else level <-"VERY HIGH"
return (rlevel)
}
k<-USArrests$UrbanPop
k
q<- quantile(USArrests$UrbanPop, c(.25,.5,.75))
newCol <- sapply(USArrests$UrbanPop,AssignLevel(k,q))
I'm trying to change the value of every state's urban pop value into one of the corresponding quartiles. It works when I run AssignLevel(k,q) but not when I run in in sapply.
I agree that the cut solution is better. For fun, here is how to resolve your current issue:
data("USArrests")
AssignLevel <- function(p,quartiles) {
if (p < quartiles[[1]]){
rlevel <- "LOW"
} else if(p < quartiles[[2]]) {
rlevel <- "MODERATE"
} else if(p < quartiles[[3]]) {
rlevel <- "HIGH"
} else {
rlevel <- "VERY HIGH"
}
return (rlevel)
}
k <- USArrests$UrbanPop
q <- quantile(k, c(.25,.5,.75))
newCol <- sapply(k,AssignLevel,q)

Resources