Preparing R for Use

Start R or R Studio and enter the following to see what packages are loaded in your R session. An R package is a set of libraries and dependencies which you can get from package repositories. Users install one or more packages from the internet and, once it has been downloaded and installed to your local computer, you must then import it as a library into R.

(.packages())

Unless you have downloaded and installed packages previously, chances are that you won’t have access to many of the libraries needed to perform the analyses we will examine today. The following code can be used to download and install each of the required packages in one simple step.

install.packages(c("data.table", "broom", "dplyr", "zoo", "sandwich", "MASS", "quadprog", "tseries", "strucchange", "fracdiff", "forecast", "iterators", "codetools", "foreach", "bfast", "raster", "sf"), repos='http://cran.us.r-project.org')

Alternatively, you can download and install packages one at at time by following this example for the data.table package:

install.packages("data.table", repos='http://cran.us.r-project.org')

Now that the required packages have been installed, you must load them in the current R session to access the capabilities, code, and documentation that each contains. You can load multiple libraries at once by creating a list of the desired libraries, then load them all at once with “lapply”.

my_packages <- c("data.table", "broom", "dplyr", "zoo", "sandwich", "MASS", "quadprog", "tseries", "strucchange", "fracdiff", "forecast", "iterators", "codetools", "foreach", "bfast", "raster", "sf")

lapply(my_packages, library, character.only = TRUE)

Extract Multivalues to Points

Next, extract each vegetation index (VI) value for the preferred landuse/landcover type to their respective centroids. This process collects the time series of VI values for each pixel in a format that we can input into the BFAST algorithm.

First, make sure the following libraries are loaded into your current R session:

library(raster)
library(sf)

Next, set the working directory to the folder that contains your satellite images to create a list of of the available images, then use the list to create a raster stack.

setwd("D:/Research/Projects/BFAST_2001_2025/MOD13Q1/NDVI")
f <- list.files(pattern = "tif$")
ras <- lapply(f, raster)
rstack <- stack(ras)

Finally, read in a shapefile containing the centroids, extract each of the underlying cell values from the raster stack, and write the output to a CSV file.

setwd("D:/Research/Projects/BFAST_2001_2025/BFAST/tiles")
tilepts <- st_read("Centroids_462.shp")  #
start_time <- Sys.time()
ext <- extract(rstack, tilepts)
write.table(ext, "D:/Research/Projects/BFAST_2001_2025/BFAST/extract/Centroids_462.csv", sep=",", col.names=FALSE)

Importing data into R, and its analysis, can be a memory-intensive operation. To alleviate potential memory problems that might slow down the system, I recommend clearing any objects in memory before moving on to the next step.

rm(list=ls())