Skip to content

Transition Matrix and Prediction Scripts #3901

Open
ananyak534 wants to merge 3 commits into
PecanProject:developfrom
ananyak534:add_transition_matrix
Open

Transition Matrix and Prediction Scripts #3901
ananyak534 wants to merge 3 commits into
PecanProject:developfrom
ananyak534:add_transition_matrix

Conversation

@ananyak534
Copy link
Copy Markdown

Pull request for my script that generates a transition matrix on crop classes for the 2018-2023 data. The second file, 'predict_and_store.R', uses the matrix to predict crop classes up until a given year for one of the four seasons.

What this does

  • Combines crop data from 2018–2023
  • Cleans up crop sequences per parcel
  • Builds a transition matrix of crop changes
  • Uses a Markov model to predict future crops (2024–2030)
  • Plots actual vs predicted for a sample parcel

Key parts

  • Transitions are counted and turned into a probability matrix (with smoothing)
  • Predictions are generated by applying the matrix and sampling next states

Outputs

  • seq_long.csv: long-format sequences
  • full_transition_matrix.csv: transition probabilities

Notes

  • Transition step is slow, so the results and files are saved and reused

  • this uses simplified Markov behavior where next state depends only on current

  • How to run

  • Run 'transition_matrix.R' once to generate CSVs (but it will take a bit of time ~20 minutes)

  • run 'predict_and_store.R' using saved files --> can update season_idx and end_year values to modify predictions

if (k == 0) {
p = rep(0, nrow(tmat)); names(p) = rownames(tmat); p[cur] = 1
} else {
p = predict_k_steps(tmat, cur, k)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend that you rethink what you're doing here to make the predictions iteratively (use the last prediction as the input to the next prediction) rather than repeating the prediction from 0 to k each time k increases by 1

start_info[, CLASS := trimws(as.character(CLASS))]
states = rownames(tmat_final)

##all predictions
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the relationship between "predictions_with_tmatrix.R" and the code in this file? It doesn't look like you're using those functions here. I'd recommend either using those functions or converting the core of this code into a function, and then have the script here focus on reading specific data, calling the prediction function, and then performing the validation. Indeed, such a script might be better reformatted as a Rmd vignette.

dlebauer pushed a commit to ashiklom/pecan that referenced this pull request Apr 22, 2026
Two bugs found in the PEcAn.uncertainty module:

1. run.sensitivity.analysis.R: if (!pfts %in% ...) uses a vector-valued
   condition, which triggers an error in R >= 4.x when more than one PFT name
   is supplied.  Wrapping with �ny() makes the intent explicit and avoids
   the multi-element condition warning/error.

2. get.results.R: The logger.warn call for the sensitivity analysis loop
   appended a literal ')' to the variable name string, producing spurious
   output such as 'Currently performing sensitivity analysis on variable NPP)'.
   Removed the stray closing parenthesis.

Refs: PecanProject#3901
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants