Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence of varying increments with R?

Tags:

r

sequence

I'm optimizing some various models, one of which is radialSVM using the caret package. I'm creating a tuning grid in preparation to cycle through a loop to find the best parameters to use for the model.

One thing that would be extremely helpful is some sort of varying increment sequence. For example, I'd like to start with small parameter values incremented in small steps. The larger I go, the bigger steps I can take. I've found that small parameters do change the model quite a bit, so I'd like to explore them more carefully.

It would be fantastic to have the sequence increment by some multiplier of the current step, say x <- x+5*x. Is this possible with something that already exists, (like a creative use of seq()), or do I need to use a loop?

like image 571
Hendy Avatar asked Aug 27 '12 04:08

Hendy


People also ask

How do you make a sequence of numbers in R?

The simplest way to create a sequence of numbers in R is by using the : operator. Type 1:20 to see how it works. That gave us every integer between (and including) 1 and 20 (an integer is a positive or negative counting number, including 0).

What is the seq function in R?

seq() function in R Language is used to create a sequence of elements in a Vector. It takes the length and difference between values as optional argument. Syntax: seq(from, to, by, length.out)

How do I print a sequence in R?

The function rep() in R will repeate a number or a sequence of numbers n times. For example, typing rep(5, 5) wiull print the number 5 five times.

How do you find the length of a sequence in R?

A sequence vector is created by using the sequence of numbers such as 1 to 15, 21 to 51, 101 to 150, -5 to 10. The length of this type of vectors can be found only by using the length function.


2 Answers

How about something like this:

0.0001 * 6^(0:10)
#  [1]    0.0001    0.0006    0.0036    0.0216    0.1296    0.7776    4.6656
#  [8]   27.9936  167.9616 1007.7696 6046.6176
like image 154
Josh O'Brien Avatar answered Sep 20 '22 22:09

Josh O'Brien


You could use the exponential distribution:

qexp((1:100)/100)

> qexp((1:100)/100)
  [1] 0.01005034 0.02020271 0.03045921 0.04082199 0.05129329 0.06187540 0.07257069 0.08338161
  [9] 0.09431068 0.10536052 0.11653382 0.12783337 0.13926207 0.15082289 0.16251893 0.17435339

Adjust it to have differences that meet you needs:

 diff( 20* qexp((1:100)/100) )
 [1]  0.2030474  0.2051300  0.2072557  0.2094260  0.2116422  0.2139058  0.2162183  0.2185814
 [9]  0.2209967  0.2234660  0.2259911  0.2285739  0.2312164  0.2339208  0.2366892  0.2395238
like image 34
IRTFM Avatar answered Sep 22 '22 22:09

IRTFM