I am creating a function to use in gridSearch:
Negs <- c()
Poss <- c()
Fun <- function(x){
for(i in 1:200){
j <- a[i]
k <- b[i]
l <- c[i]
m <- d[i]
Negs[i] <- x[1L]*j + x[2L]*k + x[3L]*l + x[4L]*m
}
for(i in 1:200){
n <- e[i]
o <- f[i]
p <- g[i]
q <- h[i]
Poss[i] <- x[1L]*n + x[2L]*o + x[3L]*p + x[4L]*q
}
print(length(Poss[Poss<=max(Negs)]))}
I then use gridSearch with this function and it works fine.
It would be useful to have the vectors Negs and Poss populated; however, this does not seem to happen. On my global environment they are empty.
Then trying (just to try and work out what is going on):
Results <- function(x){
for(i in 1:200){
j <- a[i]
k <- b[i]
l <- c[i]
m <- d[i]
Negs[i] <- x[1L]*j + x[2L]*k + x[3L]*l + x[4L]*m
cat()
}
for(i in 1:200){
n <- e[i]
o <- f[i]
p <- g[i]
q <- h[i]
Poss[i] <- x[1L]*n + x[2L]*o + x[3L]*p + x[4L]*q
}
print(Negs)
print(Poss)
}
Results(c(2,1,2,1))
This works and prints Negs and Poss, but again they are not stored in the global environment.
What am I missing?
Thanks for the help!