<jupyter_start><jupyter_text>Run modelThis runs the stun model as defined in a [Stan file](../stan_models/parabolic_iCAR.stan) provided, for the 5 environmental variables used as predictors, and a fraction of the cultivated area used as a response variable. It assumes that the model is located in the 'stan_models' directory.<jupyter_code>### Load libraries
library(here)
library(rstan)
library(rgdal)
#### Define paths:

# Inputs
path2env_sd<- here("data","Sd_Env_by_county.csv")
path2prd_counties<-here("data","Prd_by_county.csv")
path2adjacency <- here("data","adjacency.csv")
stan_model<-here("stan_models","parabolic_iCAR.stan")

# Outputs
path2model<-here("outputs","models","parabolic_iCAR.rds")
### Load data
env<-read.csv(path2env_sd) # predictors
prd<-read.csv(path2prd) # response
adjacency<-read.csv(path2adjacency) # adjacency
### Format data

# Get the adjacent nodes from the matrix:
node1<-adjacency$node1
node2<-adjacency$node2
N_edges<-length(node1)
N<-nrow(prd)

# Define stan data:
y=prd$logArea
x1=env$BIO10_sd
x2=env$BIO17_sd
x3=env$BIO4_sd
x4=env$BIO9_sd
x5=env$npp_sd

#Format the data
stan_data <- list(N = N, x1 = x1, y = y,x2=x2,x3=x3,x4=x4,x5=x5,N_edges=N_edges,node1=node1,node2=node2)
### Define model parameters:

warmup=2000
iter=10000
chains=5
cores=5
thin=1
### Run model
fit <- stan(file = stan_model, data = stan_data, warmup = warmup, iter = iter, chains = chains, cores = cores, thin = thin,control = list(max_treedepth = 15))

### Save model
saveRDS(fit, path2model)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13802083333333334, 'fraction_numerical': 0.034505208333333336, 'mean_word_length': 6.8820512820512825, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '231443', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 630, 'n_tokens_neox': 579, 'n_words': 131, 'path': 'notebooks/04_Run_model.ipynb', 'repo_name': 'mkrzyzanska/BuckwheatNicheModelling', 'size': '1536'}