# Performing trend analysis with R?

I have data of monthly temperature for 60 years.

I want to analyze the trend for the monthly temperature in R.

How do I construct R Code which would analyze monthly trend over the time series to determine spatial distribution of significant trends in monthly average and min/max data? I have this code but do not know how to adjust so

``````//get the direction and magnitude of trends,
time <- 1:nlayers(gimms.sum)
fun=function(x) { if (is.na(x[1])){ NA } else { m = lm(x ~ time); summary(m)\$coefficients[2] }}
gimms.slope=calc(gimms.sum, fun)
gimms.slope=gimms.slope*31 //number of years
plot(gimms.slope)
fun=function(x) { if (is.na(x[1])){ NA } else { m = lm(x ~ time); summary(m)\$coefficients[8] }}
p <- calc(gimms.sum, fun=fun)
plot(p, main="p-Value")
//mask all values >0.05 to get a confidence level of 95%:
m = c(0, 0.05, 1, 0.05, 1, 0)
rclmat = matrix(m, ncol=3, byrow=TRUE)
p.mask = reclassify(p, rclmat)
fun=function(x) { x[x<1] <- NA; return(x)}
p.mask.NA = calc(p.mask, fun)
//Mask insignificant trends
trend.sig = mask(gimms.slope, p.mask.NA)
plot(trend.sig, main="significant NDVI change")
``````
• This is a very broad question, too broad for this site as is. What have you tried? Do you know how to read raster data into R? How to fit models? Plot maps? Summarise raster data? – Spacedman Jun 14 at 15:58
• Dear @Spacedman, you are right, sorry for it. As it is a bit long, I edited the question. – lola Jun 14 at 19:41
• want to delete this question @PolyGeo – lola Jun 15 at 6:52
• There should be a delete button beneath it that enables you to do that. However, it would be better for you to keep editing to improve it. That way users may reverse their downvotes, and you are less likely to trigger a question ban. – PolyGeo Jun 15 at 6:55
• Welcome to GIS SE! We're a little different from other sites. We're a Q&A site, not a discussion forum. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a `{}` button that enables you to format any highlighted code nicely. Please check out our short tour for more about how the site works. Thanks. – PolyGeo Jun 15 at 7:07

## 1 Answer

I found this code which might help other people as well:

https://github.com/environmentalinformatics-marburg/heavyRain/commit/30edea7e11a1fd41f98ed397fea32c4376560891#diff-f4c1e81d0b0b30884c2c20442238b2e4

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

• Please do remove my question.... I cannot do it myself as there is no button – lola Jun 16 at 7:10