自 1984 年以来,我有几个 Tiff 文件。对于每年和该区域的每个像素,我计算了 NDVI、NBR 和 TCW 以及每年每个值的平均值,但现在当我尝试这样做时
library(modifiedmk)
library(dplyr)
mkp = function(x){mkttest(x)[[5]]} # To get the p-value of the slope
sle = function(x){mkttest(x)[[2]]} # To get the actual slope value
Sens_slope <- annual_means %>%
filter(year >= 1984 & year <= 2023) %>%
group_by(year) %>%
summarise(
p.ndvi = mkp(mean_NDVI),
s.ndvi = sle(mean_NDVI),
p.nbr = mkp(mean_NBR),
s.nbr = sle(mean_NBR),
p.tcw = mkp(mean_TCW),
s.tcw = sle(mean_TCW)
)
错误提示为
Input vector must contain at least three values
。我尝试了几种方法,如果我将低于 3 的值转为 NA,则整个结果将是 NA,如果我使用 reframe
或任何其他函数,结果是相同的,这里有什么问题?
我希望有一个表格,其中包含每年每个指数的 p 值和斜率。
根据所提供的信息,很难说出确切的问题是什么。但是,这里是一个示例,说明如何使用
sens.slope
包中的 trend
来获取每个像素和变量的斜率和 p 值。
library(tidyverse)
library(trend)
set.seed(333)
# Example dataframe
df <- data.frame(
pixelID = rep(c("a", "b", "c"), each = 45),
year = rep(seq(1980,2024,1),3),
NDVI = round(rnorm(135,100,30)),
NBR = round(rnorm(135,1000,250)),
TCW = round(rnorm(135,300,25))
)
# Function to apply sens.slope() on each variable
calculate_sen_slopes <- function(sub_df) {
ndvi_result <- sens.slope(sub_df$NDVI)
nbr_result <- sens.slope(sub_df$NBR)
tcw_result <- sens.slope(sub_df$TCW)
return(data.frame(
pixelID = unique(sub_df$pixelID),
NDVI_slope = ndvi_result$estimates,
NDVI_p.value = ndvi_result$p.value,
NBR_slope = nbr_result$estimates,
NBR_p.value = nbr_result$p.value,
TCW_slope = tcw_result$estimates,
TCW_p.value = tcw_result$p.value
))
}
# Apply the function to each pixelID group and combine results
results <- df %>%
group_by(pixelID) %>%
do(calculate_sen_slopes(.))
results
#> # A tibble: 3 × 7
#> # Groups: pixelID [3]
#> pixelID NDVI_slope NDVI_p.value NBR_slope NBR_p.value TCW_slope TCW_p.value
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 a -0.0653 0.845 -6.46 0.0227 0.625 0.0481
#> 2 b -0.617 0.200 -0.605 0.853 0.167 0.475
#> 3 c -0.222 0.475 5.79 0.0428 -0.612 0.0644
创建于 2024-06-29,使用 reprex v2.1.0