For a geometric series the ratio of successive values is constant so multiplying that ratio by the current value gives the next value.
To check whether the series is geometric we can take the ratio of each successive pair of values in the series and if those ratios are all equal the series is geometric. Since that is equivalent to checking whether their variance is zero we can do it easily using var
. Since floating point arithmetic is not exact we check whether the variance is less than eps
.
Note that is.geo
returns NA for a series of length 1 or 2 and nextValue
returns NA if is.geo
does not return TRUE.
nextValue <- function(x) {
if (!isTRUE(is.geo(x))) NA
else {
y <- tail(x, 2)
y[2]^2 / y[1]
}
}
is.geo <- function(x, eps = 1e-5) var(x[-1] / x[-length(x)]) < eps
Test
Using m
defined in the Note at the end we can append the next value to it as a new column:
cbind(m, apply(m, 1, nextValue))
giving:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 4 8 16 32 64 128
[2,] 2 4 8 16 32 64 128 256
[3,] 3 6 12 24 48 96 192 384
[4,] 1 3 9 27 81 243 729 2187
[5,] 2 6 18 54 162 486 1458 4374
[6,] 3 9 27 81 243 729 2187 6561
Also we can test each row of m
to check if is geometric:
apply(m, 1, is.geo)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
is.geo(c(1, 2, 4, 12))
## [1] FALSE
Using lm
If by the method of the link shown in the question means using lm
then we can use lm
if the series is strictly positive by noting tha that the log
of such a geometric series is arithmetic so we can fit the log of the series to 1, 2, 3, ... . If the residuals are zero which occurs when the deviance is zero then it satisfies this.
fit <- function(x) {
ix <- seq_along(x)
lm(log(x) ~ ix)
}
nextValue2 <- function(x) {
if (!isTRUE(is.geo2(x))) NA
else exp( predict(fit(x), list(ix = length(x) + 1)) )
}
is.geo2 <- function(x, eps = 1.e-5) {
if (length(x) <= 2) NA
else deviance(fit(x)) < eps
}
Note
m <- matrix(c(1L, 2L, 3L, 1L, 2L, 3L, 2L, 4L, 6L, 3L, 6L, 9L, 4L,
8L, 12L, 9L, 18L, 27L, 8L, 16L, 24L, 27L, 54L, 81L, 16L, 32L,
48L, 81L, 162L, 243L, 32L, 64L, 96L, 243L, 486L, 729L, 64L, 128L,
192L, 729L, 1458L, 2187L), 6)