计算R(或其他)中的数据梯度

时间:2017-01-10 20:15:15

标签: r

我在32Hz的30分钟内获得温度数据。 我想计算当地的温度变化率。我道歉,我不知道如何制作一个正确的可重复数据集。它需要一个矢量Time,其值与32点相同,然后增加1秒。

   P1_test<- data.frame(
        Time = sample(1:32:1),
        Temperature = rnorm(32)
    )
 ycs.prime <- diff(P1_test$Temperature)/diff(P1_test$Time)
 pred.prime <- predict(spl, deriv=1)

我得到infs和0s因为Deltat大部分时间都是0,除了秒之间的阶跃变化。你有什么建议?

1 个答案:

答案 0 :(得分:3)

正如Gregor指出的那样,如果你想看看温度如何随时间变化,你需要更精细的时间变量表示。有两种解决方案。最好的选择是如果您的数据按照记录的顺序排序(所以第一行是第一次测量,第二行是第二次测量等)然后使用订购信息做出更详细的信息时间变量的版本:

> # there are 1800 seconds in 30 minutes, 32 measurements per second
> Seconds <- as.numeric(gl(n=1800, k=32, labels=1:1800))
> Temp <- rnorm(57600)
> df <- data.frame(Seconds, Temp)
> head(df) # the first 6 rows
  Seconds       Temp
1       1 -0.9543326
2       1  0.1973152
3       1 -0.4815007
4       1 -0.2494005
5       1  0.7282253
6       1 -1.0690358
> tail(df) # the last 6 rows
      Seconds         Temp
57595    1800 -0.708576762
57596    1800  2.660348850
57597    1800 -0.003186668
57598    1800  0.025776665
57599    1800 -1.627054312
57600    1800  0.241060762
> 
> ycs.prime <- diff(df$Temp)/diff(df$Seconds) # doesn't work properly
> 
> head(ycs.prime, 35) # first 35 elements
 [1]        Inf       -Inf        Inf        Inf       -Inf       -Inf        Inf       -Inf
 [9]       -Inf        Inf       -Inf        Inf       -Inf       -Inf        Inf       -Inf
[17]       -Inf        Inf        Inf        Inf       -Inf        Inf       -Inf        Inf
[25]       -Inf       -Inf        Inf       -Inf        Inf        Inf       -Inf -0.2423703
[33]        Inf        Inf       -Inf
>

同样,假设数据框中的行是测量的正确顺序,您可以添加一个时间变量,它只是测量的顺序。它将从1(第一次测量)到57600(最后一次测量)。假设定期进行测量,该变量的单位为1/32秒。

> df$Time <- 1:nrow(df)
> 
> head(df)
  Seconds       Temp Time
1       1 -0.9543326    1
2       1  0.1973152    2
3       1 -0.4815007    3
4       1 -0.2494005    4
5       1  0.7282253    5
6       1 -1.0690358    6
> tail(df)
      Seconds         Temp  Time
57595    1800 -0.708576762 57595
57596    1800  2.660348850 57596
57597    1800 -0.003186668 57597
57598    1800  0.025776665 57598
57599    1800 -1.627054312 57599
57600    1800  0.241060762 57600
> 
> ycs.prime <- diff(df$Temp)/diff(df$Time)
> plot(ycs.prime, type = "l") 

temperature change

想要将此变量转换为更易于解释的单位吗?

> df$Time <- df$Time/32 # now it's in seconds

如果您不确定行是否按顺序,那么您实际上并没有32Hz的信息,每秒有32个样本,但您不知道它们的顺序是什么进来了。你能做的最好的事情就是平均你每秒钟的32个样本,以便每秒获得一个更可靠的测量值,然后观察温度从秒到秒的变化。

> # again, same initial data frame
> Seconds <- as.numeric(gl(n=1800, k=32, labels=1:1800))
> Temp <- rnorm(57600)
> df <- data.frame(Seconds, Temp)
> 
> # average Temp for each Second
> df$Temp <- ave(df$Temp, df$Seconds, FUN = mean)
> head(df) # note it's the same for the whole first second
  Seconds      Temp
1       1 0.1811943
2       1 0.1811943
3       1 0.1811943
4       1 0.1811943
5       1 0.1811943
6       1 0.1811943
> df <- unique(df) # drop repeated rows
> nrow(df) # one row per second
[1] 1800
> 
> ycs.prime <- diff(df$Temp)/diff(df$Seconds)
> plot(ycs.prime, type = "l")

temperature change by second