logo
down
shadow

Format and export the output of Mann-Kendall test in R to excel from Rstudio


Format and export the output of Mann-Kendall test in R to excel from Rstudio

Content Index :

Format and export the output of Mann-Kendall test in R to excel from Rstudio
Tag : r , By : user98986
Date : January 11 2021, 05:14 PM

may help you . I've just started using R and would like to use the trend package to perform Mann-Kendall tests on yearly groundwater level data of multiple wells. Here is a sample from my dataframe pri.csv: , An option would be tidy from broom
library(broom)
library(purrr)
library(trend)
map_dfr(results_list[!bad], tidy)
out <- do.call(rbind, lapply(results_list[!bad], function(x) 
  cbind(data.frame(p.value =  x$p.value, statistic  = x$statistic), 
          data.frame(as.list(x$estimates)))))
row.names(out) <- NULL


out
#    p.value statistic  S     varS        tau
#1 1.0000000  0.000000  1 3.666667  0.3333333
#2 1.0000000  0.000000  1 3.666667  0.3333333
#3 0.2962699 -1.044466 -3 3.666667 -1.0000000
#4 1.0000000  0.000000 -1 3.666667 -0.3333333

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Split vector and apply Mann Kendall test


Tag : r , By : user186435
Date : March 29 2020, 07:55 AM
I hope this helps you . I have a long vector of financial time series information, price and volume. I expand the price vector based on the volume vector so I essentially get a price per share traded, and so only one vector is analyzed. I would like to split this vector into lengths of 100 (could be any number) each, and apply the Mann-Kendall trend analysis on each of these new vectors. Below is the code I have for this. , Using lapply for example, here I do it only for the first 5 elements.
   lapply(d1[1:5],MannKendall)
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
WARNING: Error exit, tauk2. IFAULT =  12
$`1`
tau = 1, 2-sided pvalue =1

$`2`
tau = 1, 2-sided pvalue =1

$`3`
tau = 1, 2-sided pvalue =1

$`4`
tau = 1, 2-sided pvalue =1

$`5`
tau = 1, 2-sided pvalue =1
   do.call(rbind,lapply(d1[1:5],function(x)unlist(MannKendall(x))))

  tau sl S D varS
1   1  1 0 0    0
2   1  1 0 0    0
3   1  1 0 0    0
4   1  1 0 0    0
5   1  1 0 0    0

Autocorrelation and Mann-Kendall Trend test for multiple timeseries


Tag : r , By : user183442
Date : March 29 2020, 07:55 AM
I hope this helps you . My above comments aside I think this will get you what you're looking for:
stationList <- unique(df$stn_num)
resultsList <- vector("list", length(stationList))
for(i in stationList){
  tempDF <- df[df$stn_num == i, ]
  c<-acf(tempDF$value,lag.max=1)
  t <- dim(c$acf)
  tempDF$prewhit1<-c$acf[[t[1], t[2], t[3]]]*tempDF$value
  prewhitseries<-data.frame(with(tempDF, (tempDF$value[-1] - prewhit1[-length(prewhit1)])))
  autocordata<-cbind(tempDF[-1,],prewhitseries)
  resultsList[[grep(i, stationList)]] <- MannKendall(autocordata[,5])
}
names(resultsList) <- stationList

Speed-up a parallel process calculating a mann-kendall test over a huge dataset in R


Tag : r , By : oiyto
Date : March 29 2020, 07:55 AM
help you fix your problem You note that you have already fixed your problem. Is obtainable using one of the following steps:
1: Copy the necessary objects to the foreach loops using .packages and .export. This ensures that each instance will not clash when trying to access the same memory.
set.seed(123)
data<- data.frame(cbind(runif(10000,-180,180), runif(10000,-90,90))
                  , replicate(1200, runif(10000,0,150)))

coords<-data[,1:2] #get the coordinates out of the initial dataset
names(coords)<-c("lon","lat") 
data_t<- as.data.frame(t(data[,3:1202])) #each column is now the time series associated to a point
data_t$month<-rep(seq(1,12,1),100) # month index as last column of the data frame
# start the parallel processing

library(data.table)
library(parallel)
library(trend)
setDT(data_t)
setDT(coords)
cores=detectCores() #count cores
cl <- makeCluster(cores[1]-1) #take all the cores minus 1 not to overload the pc

#user  system elapsed 
#17.80   35.12   98.72
system.time({
  test <- data_t[,parLapply(cl, 
                            .SD, function(x){
                              (
                                unlist(
                                  trend::mk.test(x)[c("p.value","statistic","estimates")]
                                )
                               )
                              }
                            ), by = month] #Perform the calculations across each month
  #create a column that indicates what each row is measuring
  rows <- rep(c("p.value","statistic.z","estimates.S","estimates.var","estimates.tau"),12)

  final_tests <- dcast( #Cast the melted structure to a nice form
                      melt(cbind(test,rowname = rows), #Melt the data for a better structure
                        id.vars = c("rowname","month"), #Grouping variables
                        measure.vars = paste0("V",seq.int(1,10000))), #variable names
                      month + variable ~ rowname, #LHS groups the data along rows, RHS decides the value columns
                      value.var = "value", #Which column contain values? 
                      drop = TRUE) #should we drop unused columns? (doesnt matter here)
  #rename the columns as desired
  names(final_tests) <- c("month","variable","S","tau","var","p.value","z_stat")
  #finally add the coordinates
  final_tests <- cbind(final_form,coords) 
})

How do you perform Mann-kendall test on multiple stations using loops?


Tag : r , By : TheMoo
Date : March 29 2020, 07:55 AM
To fix the issue you can do Something like this should do. split() splits along the well column, creating a list with a vector for each well. Only vectors of length 3 or more are kept. MannKendall() is then run on each of the remaining vectors using lapply()
library(Kendall)

tt <- read.table(text="
well    year    month   value
684     1994    Jan     8.53
684     1995    Jan     8.74
684     1996    Jan     8.88
684     1997    Jan     8.24
1001    2000    Jan     9.1
1001    2001    Jan     9.2
1001    2002    Jan     9.54
1001    2003    Jan     9.68
2003    1981    Jan     55.2
2003    1982    Jan     55.8
2003    1983    Jan     56.4
2003    1984    Jan     53.2
2004    1984    Jan     53.2", header=TRUE)

tt.wells <- split(tt$value, tt$well)
tt.wells <- tt.wells[lengths(tt.wells) >= 3]

lapply(tt.wells, MannKendall)

# $`684`
# tau = 0, 2-sided pvalue =1

# $`1001`
# tau = 1, 2-sided pvalue =0.089429

# $`2003`
# tau = 0, 2-sided pvalue =1

How to perform mann kendall trend test on multiple levels


Tag : r , By : Jeskl
Date : March 29 2020, 07:55 AM
it should still fix some issue I am working with station precipitation data. Each station has precipitation data for 60 years and there are 30 stations. I want to perform a Mann Kendall trend test on each station to see if there is a significant trend for precipitation. , @A.Suliman, you're right.
This seems to work:
ID<-c(1,1,1,1,1,2,2,2,2,2)
prcp<-c(2,0,1,4,5,0,2,3,5,6)
df<-cbind(ID,prcp)

mk<-df %>%
  as.data.frame() %>% 
  group_by(ID) %>%
  summarise(prcpmk=MannKendall(prcp)$sl)
Related Posts Related QUESTIONS :
  • Conditionally replace the values in columns to value in another column using dplyr
  • Add count as label to points in geom_count
  • Temporarily Disable Rprofile
  • Select certain region of column for lm
  • Convert multiple rows into one row depending on unique values in another column
  • Issues installing Plotly Dash for R
  • Is there an R function to retrieve values from a matrix of column names?
  • R;Too slow to overate loops for million vectors
  • How to optimize intersect of rows and columns in a matrix?
  • how to calculate cumsum with depreciation in a grouped dataframe?
  • reshape wide to long based on part of column name
  • How to get a hyperlink for the words in a description in an r dataframe?
  • shinymeta works locally but breaks when published to shinyapps.io
  • Deparse and (un)escape quotes
  • Regression table with clustered standard errors in R jupyter notebook?
  • Disaggregate quarterly data to daily data in R keeping values?
  • How to save output to console and file simultaneously in RStudio server?
  • Why does data.table j have a different environment when directly calling mget() vs calling mget() inside another functio
  • scale_fill_viridis_c color bar on a log scale
  • How to change the lab name corresponding to function in ggplot
  • R, filtering for an element in a list in a dataframe cell
  • Extracting only bottom temperature from 4d NetCDF file
  • How to add/wrap lines of text to .tex with .sh script
  • R - building new variables from sequenced data
  • Sum rows values one after the other
  • Nesting ifelse inside summarytools
  • How best to divide different levels of a factor by one another in dataframe in R?
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • Why manual autocorrelation does not match acf() results?
  • Merge 3 dataframes which are different to each other
  • remove adjacent duplicates from string
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com