【问题标题】:"Approximating" the derivative of date points in R“近似”R中日期点的导数
【发布时间】:2012-05-03 05:22:59
【问题描述】:

所以我有一个 MODIS NDVI 值的时间序列(非地理极客的植被值从 0 到 1),我试图通过使用 for 循环来近似导数。

这是我的数据样本:

> m2001
   date  value    valnorm
1     1 0.4834 0.03460912
2    17 0.4844 0.03664495
3    33 0.5006 0.06962541
4    49 0.4796 0.02687296
5    65 0.5128 0.09446254
6    81 0.4915 0.05109935
7    97 0.4664 0.00000000
8   113 0.5345 0.13864007
9   129 0.8771 0.83611564
10  145 0.9529 0.99043160
11  161 0.9250 0.93363192
12  177 0.9450 0.97434853
13  193 0.9491 0.98269544
14  209 0.9434 0.97109121
15  225 0.9576 1.00000000
16  241 0.8992 0.88110749
17  257 0.9115 0.90614821
18  273 0.8361 0.75264658
19  289 0.5725 0.21600163
20  305 0.5188 0.10667752
21  321 0.5467 0.16347720
22  337 0.5484 0.16693811
23  353 0.5427 0.15533388
  • 第 1 列是像素值的儒略日
  • 第 2 列是原始 NDVI 值
  • 第 3 列是从 0 到 1 的 NDVI(这是一种标准化技术,因为 NDVI 实际上很少达到 1 或 0)。

我对编程和 R 还是很陌生,但我想我已经设法拼凑出对它的薄弱掌握。我要做的是创建一个新列,其中的值可以让我对数据点的局部斜率有所了解。

我想出的功能是这样的:

deriv <- function(x1=1:23, x2=1){
    for (i in x1){
    i1 <- c(x1[i-1], x1[i], x1[i+1])
    i2 <- c(x2[i-1], x2[i], x2[i+1])
        deriv.func <- lm(i2~i1, na.action=NULL)
    } return(deriv.func$coef[[2]])
}

当我运行它时会发生什么:

> deriv <- function(x1=1:23, x2=1){
+ for (i in x1){
+     i1 <- c(x1[i-1], x1[i], x1[i+1])
+     i2 <- c(x2[i-1], x2[i], x2[i+1])
+ deriv.func <- lm(i2~i1, na.action=NULL)
+ } return(deriv.func$coef[[2]])
Error: unexpected symbol in:
"deriv.func <- lm(i2~i1, na.action=NULL)
} return"
> }
Error: unexpected '}' in "}"
>

我不确定我做错了什么,因为当我为 i 填写一个值时我可以解析它

> i=6
> x1=m2001$date
> x2=m2001$valnorm
>     i1 <- c(x1[i-1], x1[i], x1[i+1])
>     i2 <- c(x2[i-1], x2[i], x2[i+1])
> i1
[1] 33 49 65
> i2
[1] 0.06962541 0.02687296 0.09446254
> lm(i2 ~ i1)

Call:
lm(formula = i2 ~ i1)

Coefficients:
(Intercept)           i1  
  0.0256218    0.0007762  

> func <- lm(i2 ~ i1)
> func$coef[[2]]
[1] 0.0007761604

有什么想法吗?非常感谢。

【问题讨论】:

    标签: r function for-loop time-series derivative


    【解决方案1】:

    尝试将 'return' 换行。

        } 
        return(deriv.func$coef[[2]])
    }
    

    【讨论】:

    • 这部分解决了问题!谢谢!它现在输入没有错误,但是当我运行它时:&gt; deriv(m2001$date,m2001$valnorm) 我得到了这个:Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : NA/NaN/Inf in foreign function call (arg 1) 我感觉它与数据类型或循环有关。
    【解决方案2】:

    好吧,在查看了(很多)for 循环之后,我让它做我想做的事。

    deriv <- function(x1=1:23, x2=1){
      n=length(x1)
      deriv.func <- character(length = n)
        for (i in 1:n) {
        i1 <- c(x1[i-1], x1[i], x1[i+1])
        i2 <- c(x2[i-1], x2[i], x2[i+1])
            derivate <- lm(i2~i1)
            deriv.func[i] <- derivate$coef[[2]]*
        }
      return(deriv.func)
    }
    

    感谢@dbaseman 的帮助和正确方向的提示!
    带来改变的想法:

    • 确保为迭代器 deriv.func <- character(length = n) 分配了空间。
    • 确保中间变量 没有覆盖输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 2018-09-29
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多