0

I've created a custom function to calculate values based on two inputs.

# function
info.theta <- function(theta, delta) {
    P = 1/(1+exp(-1*(theta-delta)))
    Q = 1 -P
    1*P*Q
}

I'd like to use the function to calculate the value for all possible combinations of values for two sequences of interest.

# for each input of the function create sequences of values to explore
thetas <-  seq(-4, 4, by = .5)
deltas <- seq(-4, 4, by = .5)

I'd like to end up with a data frame with a column labeled thetas, deltas and information, where both theta and delta are the values for the sequence that were used in the function, and information is the output of the function for each combination of theta and delta.

I'm at a loss for how to execute the last point, as this level of coding is new to me. My hunch was maybe a nested for loop. This is obviously not correct, but it is as close as I can get to a start. How would I use the function in the way I described to generate the desired data frame?

#nested for loop

y <- NULL
for(i in sequence) {
    for(j in deltas) {
    tmp <- info.theta(i, j)
    y <- rbind(y, tmp)  
    }
}
y
1
  • 1
    Yeah, loops aren't super fun once things start to blow up. What if you had to compare n things? Commented Mar 20, 2017 at 20:06

2 Answers 2

2

You can use outer to get matrix of values:

outer(thetas,deltas,info.theta)
Sign up to request clarification or add additional context in comments.

Comments

1

A slight change to your original function:

info.theta <- function(theta, delta) {
  P = 1/(1+exp(-1*(theta-delta)))
  Q = 1 -P
  data.frame(theta=theta,delta=delta, information=1*P*Q)

}

Because data.frames are cooler.

Now:

td_grid<-expand.grid(thetas, deltas)
info.theta(td_grid[,1],td_grid[,2])

results in:

    theta delta  information
1    -4.0  -4.0 0.2500000000
2    -3.5  -4.0 0.2350037122
3    -3.0  -4.0 0.1966119332
4    -2.5  -4.0 0.1491464521
5    -2.0  -4.0 0.1049935854
6    -1.5  -4.0 0.0701037165
7    -1.0  -4.0 0.0451766597
8    -0.5  -4.0 0.0284530239
9     0.0  -4.0 0.0176627062
10    0.5  -4.0 0.0108662297
11    1.0  -4.0 0.0066480567

2 Comments

Perfect. Hopefully as I progress with my coding I will learn to articulate my aims better, and be able to more easily solve these kinds of issues before coming here. 'expand.grid' seems like a very easy fix I could have found if I could articulate my question more clearly.
Yeah I added a comment above, but I'll include it here to. Really make an effort to avoid loops where-ever possible. You can make much cleaner, effective, and faster code using the apply() group of functions. Also, if you want to get fancier, you could probably include the expand.grid() piece into the function, such that it just takes in two vectors and outputs a data.frame to your liking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.