"逼近" R中日期点的导数 [英] "Approximating" the derivative of date points in R
问题描述
来近似导数
循环。这是我的数据的样本:
> m2001
日期值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
$ c
- 第1列是像素值的茱莉亚一天
- 列2是原始的NDVI值第3列是从0-1延伸的NDVI(这是一种归一化技术,因为NDVI很少是真正的y得到1或0)。
我对编程和R还是很新的,但我想我已经设法拼凑在一起。我想要做的是创建一个新的列值,这将使我对数据点的局部斜率的一些想法。
函数我已经出现这是:
$ p $ deriv < - function(x1 = 1:23,x2 = 1){
for (x1 [i-1],x1 [i],x1 [i + 1])$ b $ b i2 < - c(x2 [i-1 ],x2 [i],x2 [i + 1])$ b $ b deriv.func <-lm(i2〜i1,na.action = NULL)
} return(deriv.func $ coef [ 2]])
}
当我运行它时会发生什么:
> (x1){
+ i1 <-c(x1 [i-1],x1 [i(x))的导函数函数(x1 = 1:23,x2 = 1){
+ (x2 [i-1],x2 [i],x2 [i + 1])$ b $ b + deriv.func < lm(i2〜i1,na.action = NULL)
+} return(deriv.func $ coef [[2]])
错误:
中的意外符号deriv.func< ; - lm(i2〜i1,na.action = NULL)
}返回
> }
错误:}中出现意外的}
>
我不知道我做错了什么,因为我可以解析它填写我的价值
> i = 6
> x1 = m2001 $ date
> x2 = m2001 $ valnorm
> i1 < - c(x1 [i-1],x1 [i],x1 [i + 1])$ b $ b> i2 <-c(x2 [i-1],x2 [i],x2 [i + 1])$ b $ b> i1
[1] 33 49 65
> i2
[1] 0.06962541 0.02687296 0.09446254
> lm(i2〜i1)
电话:
lm(公式= i2〜i1)
系数:
(截距)i1
0.0256218 0.0007762
> func <-lm(i2〜i1)
> func $ coef [[2]]
[1] 0.0007761604
谢谢。
那么,在
$ b $ pre $ deriv < - 函数(x1 = 1:23)code>循环,我得到它做我想要的。 ,x2 = 1){
n = length(x1)
deriv.func< - character(length = n)
for(i in 1:n){
i1< ; x1 [i-1],x1 [i],x1 [i + 1]),b2 [i-1],x2 [i + 1 ])
导数< -lm(i2〜i1)
deriv.func [i]< - 导数$ coef [2]] *
}
return(deriv .func)
$ b感谢您的帮助, @dbaseman!
创意有所不同:
- 确保为迭代器<$ c分配了空间$ c> deriv.func < -
character(length = n)
。
确保中间变量
没有覆盖输出。
So I have a time series of MODIS NDVI values (vegetation values from 0-1 for the non-geographic geeks), and I'm trying to approximate the derivative by using a for
loop.
This is a sample of my data:
> 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
- Column 1 is the julian day of the pixel value
- Column 2 is the raw NDVI value
- Column 3 is the NDVI stretched from 0-1 (it's a normalization technique, since NDVI rarely actually gets to 1 or 0).
I'm still very new to programming and R, but I think I've managed to piece together a tenuous grasp on it. What I'm trying to do is create a new column with values that would give me some idea of the local slope of data points.
The function I've come up with is this:
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]])
}
What happens when I run it is this:
> 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'm not sure what I'm doing wrong, as I can get it to parse when I fill in a value for 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
Any ideas? Thanks a ton.
解决方案 Well, after looking (a lot) more into the for
loop, I got it to do what I want.
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)
}
Thanks for the help, and the tip in the right direction, @dbaseman!
Ideas that made a difference:
- making sure I had space allocated for the iterator
deriv.func <-
character(length = n)
.
- making sure the intermediate variables
didn't overwrite the output.
这篇关于"逼近" R中日期点的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!