# checking whether I wrote a formula correctly in R

#### gianmarco

##### TS Contributor
Hello,
I have been off the grid for some time. It's nice to post again something, and I look forward to hearing from you

As per title, I am wondering if I wrote correctly a formula in R.
The formula, as derived from literature, is attached as an image. It aims at predicting the speed of walking (m/s) as function of the terrain slope (slope measured in percent).

Now, the way I trasposed it into R is the following:
C-like:
(0.11 + 0.67 * exp(-(abs(x[adj])*100 + 2)^2 / (2 * 30^2))) * 3.6
Now, the following bit
is the slope in percent, while the multiplication by 3.6 is meant to convert the speed from m/s to Kmh.

Since I am getting weird results, I am wondering if I screwed something up in the formula.

Any feedback is really apprecited.
Best
Gm

#### Attachments

• 14.8 KB Views: 6

#### spunky

##### Can't make spagetti
Have you tried starting with the formula as originally intended, see if it make sense, then add the 3.6 factor part, see if that makes sense and finally the abs(x[adj])*100?

Btw, the abs(x[adj])*100 seems peculiar to me. If something's weird I'd be willing to say that's where it is.

#### gianmarco

##### TS Contributor
Thanks Spunky....
what seems peculiar to you is the only thing I know is correct LOL.

Forget that, and take it as the "slope" value...

#### Buckeye

##### Active Member
Code:
speed<-function(X){

(0.11 + (0.67 * exp((-(X + 2)^2) / (2 * 30^2))))

}
Does something like this work? And then define X as the slope. Perhaps easier to read?

#### Dason

It wouldn't be a bad idea to show us some of you input data and what the expected output would be. Right now we are just operating under your assessment that something is wonky without knowing what actually is wrong.

#### spunky

##### Can't make spagetti
Thanks Spunky....
what seems peculiar to you is the only thing I know is correct LOL.

Forget that, and take it as the "slope" value...
I guess I gravitated towards that part of the equation because you're mentioning "weird results" and I'm thinking "OK, if the results are "weird" the only modification from the formula where I could see this coming from is that 100 part, because (at least when comparing what you did to what's there in the original) you're increasing (decreasing because of the " - "?) an exponentiated quantity by a factor of 100.

But I guess Dason is right. We don't really know what "weird" is for you, so we need to know what you'd expect VS what you're getting to try and locate where "weird" is coming from. Like, I dunno, silly example... if you say something like "Oh noes, most of my results are negative! Then we know the're some interplay between the parentheses and the negative sign that might be creating this.

When I read your "weird" and compared what you're doing with what's in the formula, my immediate intuition was "I bet he's getting results that are a lot larger/smaller than what the standard version of the formula dictates". But I guess it'd be a better idea to hear what you think is "weird" VS what we imagine you mean by it.

#### GretaGarbo

##### Human
It doesn't look that crazy does it?

Code:
> slope <- seq(from=0, to=80, by= 1)
> slope
[1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[25] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
[49] 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
[73] 72 73 74 75 76 77 78 79 80
> speed <- (0.11 + 0.67 * exp(-(abs(slope  + 2)^2 / (2 * 30^2) )) * 3.6)
> round(speed, 1)
[1] 2.5 2.5 2.5 2.5 2.5 2.5 2.4 2.4 2.4 2.4 2.3 2.3 2.3 2.2 2.2 2.2 2.1 2.1
[19] 2.0 2.0 2.0 1.9 1.9 1.8 1.8 1.7 1.7 1.6 1.6 1.5 1.5 1.4 1.4 1.3 1.3 1.2
[37] 1.2 1.1 1.1 1.1 1.0 1.0 0.9 0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.6 0.6 0.6 0.6
[55] 0.5 0.5 0.5 0.5 0.4 0.4 0.4 0.4 0.4 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.2 0.2
[73] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
> plot(slope, speed)
(It looks like a logistic.)

But a walking speed of 2.5 km/h at zero slope sounds a bit slow.

(And it is nice to see @gianmarco here again.)

#### gianmarco

##### TS Contributor
Hello All,
Yes you are right, I should have provided more context. The problem is that that formula is out from a larger code that I have used in a package of mine that has been released to CRAN abut 1 years ago (https://cran.r-project.org/web/packages/movecost/index.html), and it would be tricky to provide a workable example.

I thought that there was an issue in the way I translated the formula into ; sometimes I get confused by the round parentheses

In any case, I have found that the error was in a subsequent part of my code, and I have just fixed it.

I attach a picture that is produced by a function in my package.
To the right-hand side, there is a digital terrain model of a small vulcano in New Zealand; the black dot represent the start location for human walking, the red dots are destination locations. The numeric labels close to the destinations are the time it takes to go from the origin to the destinations buy walking, considering the walking speed as function of the slope of the terrain.

To the left-hand side, there is the accumulated cost surface around the origin, with contours indicating walking time (in hour).

Hope this may prove interesting.

Thanks again.
Cheers

#### Attachments

• 604.2 KB Views: 2

#### gianmarco

##### TS Contributor
It doesn't look that crazy does it?

Code:
> slope <- seq(from=0, to=80, by= 1)
> slope
[1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[25] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
[49] 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
[73] 72 73 74 75 76 77 78 79 80
> speed <- (0.11 + 0.67 * exp(-(abs(slope  + 2)^2 / (2 * 30^2) )) * 3.6)
> round(speed, 1)
[1] 2.5 2.5 2.5 2.5 2.5 2.5 2.4 2.4 2.4 2.4 2.3 2.3 2.3 2.2 2.2 2.2 2.1 2.1
[19] 2.0 2.0 2.0 1.9 1.9 1.8 1.8 1.7 1.7 1.6 1.6 1.5 1.5 1.4 1.4 1.3 1.3 1.2
[37] 1.2 1.1 1.1 1.1 1.0 1.0 0.9 0.9 0.9 0.8 0.8 0.7 0.7 0.7 0.6 0.6 0.6 0.6
[55] 0.5 0.5 0.5 0.5 0.4 0.4 0.4 0.4 0.4 0.3 0.3 0.3 0.3 0.3 0.3 0.3 0.2 0.2
[73] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
> plot(slope, speed)
(It looks like a logistic.)

But a walking speed of 2.5 km/h at zero slope sounds a bit slow.

(And it is nice to see @gianmarco here again.)
Thanks @GretaGarbo for welcoming me back.
By the way, the formula we discussed about regarding walking speed is for off-road walk...that could account for such slow speed at slope 0.

Best