Skip to content

Commit fa65468

Browse files
committed
Initiation function for even harvest #13
Added initiation distrib from issue #13 on github Also edited the tests because new def_init is causing test fails
1 parent d721a95 commit fa65468

18 files changed

+400
-16
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ LazyData: true
4545
LazyDataCompression: bzip2
4646
URL: https://github.com/gowachin/matreex
4747
Roxygen: list(markdown = TRUE)
48-
RoxygenNote: 7.2.3
48+
RoxygenNote: 7.3.1

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export(def_init)
4242
export(def_initBA)
4343
export(def_init_even)
4444
export(def_init_k)
45+
export(def_init_planting)
4546
export(delay)
4647
export(disturb_fun)
4748
export(disturb_fun_mixt)

R/class_species.R

+31
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,37 @@ def_init_k <- function(x){
428428
return(fun)
429429
}
430430

431+
#' Init population from plantation, after even clear cut
432+
#'
433+
#' @param species name of the species. This allows to extract the parameters
434+
#' from matreex::distrib_planting dataset, that have been computed by Georges in
435+
#' issue #13.
436+
#'
437+
#' @details
438+
#' This function may replace def_init_even() in the long term.
439+
#'
440+
#'
441+
#' @export
442+
def_init_planting <- function(species){
443+
# dev
444+
# species <- "Picea_abies"
445+
distrib <- matreex::distrib_planting
446+
pms <- distrib[distrib$species == species,]
447+
if(nrow(pms) != 1){
448+
stop("This species does not retrieve any row from matreex::distrib_planting dataset. Check the format of the species column.")
449+
}
450+
force(params)
451+
fun <- function(mesh, SurfEch = 0.03) {
452+
453+
x <- dnorm(log(mesh), mean = pms$mean_DBH_log, sd = pms$sd_DBH_log)/
454+
(sum(dnorm(log(mesh), mean = pms$mean_DBH_log, sd = pms$sd_DBH_log)))
455+
x[x < 1e-11] <- 0 # this teshold is the same as the IPM minimal non null values
456+
457+
return(x * SurfEch)
458+
}
459+
460+
return(fun)
461+
}
431462

432463
#' Default population harvest
433464
#'

R/distrib_planting.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#' Species distribution parameter for young stands.
2+
#'
3+
#' @name distrib_planting
4+
#'
5+
#' @source Georges Kunstler
6+
#'
7+
"distrib_planting"

R/matreex.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
#'
88
#' @name matreex
99
#' @docType package
10-
NULL
10+
"_PACKAGE"

data/distrib_planting.rda

2.35 KB
Binary file not shown.

dev/ParamSizeDistYoungStand.csv

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"","species","mean_DBH_log","sd_DBH_log","Nha","BAha"
2+
"1","Abies alba",4.70218549577077,0.0495809812130126,517.661497058824,5.41172158515756
3+
"2","Acer campestre",4.69021478810264,0.0229491520261313,206.183766666667,1.9017154702442
4+
"3","Acer pseudoplatanus",4.7110533387838,0.037044737558841,442.281173333333,4.481553224364
5+
"4","Alnus glutinosa",4.72228044928083,0.0627169270786061,369.777573707849,4.03433579604816
6+
"5","Alnus sp",4.71078238563064,0.0339154927124127,842.02096,8.79999945002538
7+
"6","Betula sp",4.71352082053515,0.0463938807874255,282.29810822709,2.93144266175903
8+
"7","Carpinus betulus",4.70842261081656,0.0581175873300147,331.342175357143,3.53293192490761
9+
"8","Castanea sativa",4.71322723646938,0.0576733957110027,625.385746924825,6.5825770483407
10+
"9","Eucalyptus camaldulensis",4.72113227559557,0.0836156442799526,288.196760443228,3.04503871031746
11+
"10","Eucalyptus globulus",4.73635502053714,0.0985885082207965,447.105139686778,4.91044956388888
12+
"11","Fagus sylvatica",4.71471148673444,0.0625589907059087,604.240923948785,6.4944952337039
13+
"12","Fraxinus angustifolia",4.67689121819777,0.197738049873788,163.716623610466,3.51404610058426
14+
"13","Fraxinus excelsior",4.6956048830764,0.0297308087373162,314.217673577297,3.13943794613797
15+
"14","Juniperus oxycedrus",4.70322258115659,0.0314976991059382,193.277400741428,1.91874865612117
16+
"15","Juniperus thurifera",4.71800125274837,0.102156348647792,272.445577438099,2.95473192131547
17+
"16","Larix decidua",4.70118933007119,0.0663804484729467,400.1715,3.87032614001525
18+
"17","Larix kaempferi",4.74655021548501,0.0523477139968134,1272.01855,13.2804700521601
19+
"18","Olea europaea",4.72329762114183,0.0842188126502913,287.657823069796,3.07421186342592
20+
"19","Picea abies",4.71391698732338,0.0549258868794927,554.30328116423,5.81616466566444
21+
"20","Picea sitchensis",4.68447641748224,0.0277434397471149,352.89115,3.45317349617457
22+
"21","Pinus contorta",4.72205093735784,0.075883711426374,392.582192960009,4.15032430555556
23+
"22","Pinus halepensis",4.73065495555649,0.116309449280215,344.17175202085,3.78384832192317
24+
"23","Pinus nigra",4.71770137770318,0.0874038286224909,422.0333604511,4.53370277221821
25+
"24","Pinus pinaster",4.70908191963463,0.0597032021328669,387.877221793085,4.04038045893054
26+
"25","Pinus pinea",4.74932247954807,0.0687127671922933,324.132515576554,3.51209450569149
27+
"26","Pinus radiata",4.76502411150768,0.142227689390037,477.464829275685,5.36547499999999
28+
"27","Pinus sylvestris",4.72355624381171,0.0611146710645294,287.184169902517,3.05368625086921
29+
"28","Pinus uncinata",4.71558595746972,0.0568791860360685,223.594503705949,2.26539015861161
30+
"29","Populus nigra",4.69794613568356,0.0271635322521335,284.3002,2.58986559174726
31+
"30","Populus sp",4.70646107072577,0.0543639300819205,470.006666666667,4.55245740649722
32+
"31","Populus tremula",4.71721661636695,0.0388965259535102,283.873404787988,2.91486038731533
33+
"32","Prunus padus",4.65554525240519,0.0020745190845443,113.04,1.00437331580267
34+
"33","Pseudotsuga menziesii",4.72362157464496,0.0559649421456623,551.975063432745,5.69872716068797
35+
"34","Quercus faginea",4.71653756548537,0.0731074159796038,385.107342938483,4.10933972785039
36+
"35","Quercus ilex",4.71797541980459,0.0769973139597457,409.234076051567,4.31240975661893
37+
"36","Quercus petraea",4.72275057112809,0.0576060013731273,587.732822466617,6.14638891217109
38+
"37","Quercus pubescens",4.72620358488652,0.0659217683414891,440.365722298375,4.75538259486895
39+
"38","Quercus pyrenaica",4.72764545638401,0.0967788182623929,471.27761374457,5.12762126577823
40+
"39","Quercus robur",4.71854679648392,0.0542881150244748,535.488522923456,5.61847598446725
41+
"40","Quercus rubra",4.7025161285923,0.0391011708167626,543.47855,5.47228416031546
42+
"41","Quercus suber",4.72519630147892,0.142016971915139,172.97195000328,2.10987500462963
43+
"42","Robinia pseudacacia",4.71226747547154,0.0470528425569732,424.696153846153,4.4769811823802
44+
"43","Salix caprea",4.72083877933273,0.0413963352956474,257.442778048919,2.69437893753388
45+
"44","Salix sp",4.71678579103847,0.0333416179745275,533.486066666667,5.33333257535536
46+
"45","Tilia cordata",4.62359195460859,0,88.4100000000001,0.720430709203283

dev/dev_jasper_b.Rmd

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: "Debug cases from Jasper"
3+
output:
4+
github_document
5+
---
6+
7+
```{r, include = FALSE}
8+
knitr::opts_chunk$set(
9+
collapse = TRUE,
10+
comment = "#>"
11+
)
12+
options(cli.progress_show_after = 600)
13+
# 10 minutes before showing a cli progress bar
14+
15+
library(tidyr)
16+
```
17+
18+
# Common
19+
20+
```{r make_IPM_Fagus}
21+
# library(matreex)
22+
load_all()
23+
library(dplyr)
24+
library(ggplot2)
25+
26+
# Load fitted model for a species
27+
# fit_species # list of all species in dataset
28+
data("fit_Fagus_sylvatica")
29+
30+
# Load associated climate
31+
data("climate_species")
32+
climate <- subset(climate_species, N == 2 & sp == "Fagus_sylvatica", select = -sp)
33+
# see ?climate_species to understand the filtering of N.
34+
climate
35+
36+
Fagus_ipm <- make_IPM(
37+
species = "Fagus_sylvatica",
38+
climate = climate,
39+
fit = fit_Fagus_sylvatica,
40+
clim_lab = "optimum clim",
41+
mesh = c(m = 700, L = 90, U = get_maxdbh(fit_Fagus_sylvatica) * 1.1),
42+
BA = 0:80, # Default values are 0:200, smaller values speed up this vignette.
43+
verbose = TRUE
44+
)
45+
```
46+
47+
```{r make_IPM_sylvatica}
48+
data("fit_Abies_alba")
49+
Abies_ipm <- make_IPM(
50+
species = "Abies_alba",
51+
climate = climate, # this variable is defined at the top of the doc.
52+
fit = fit_Abies_alba,
53+
clim_lab = "optimum clim",
54+
mesh = c(m = 700, L = 90, U = get_maxdbh(fit_Abies_alba) * 1.1),
55+
BA = 0:80, # Default values are 0:200, smaller values speed up this vignette.
56+
verbose = TRUE
57+
)
58+
```
59+
60+
```{r hide warn, echo=FALSE}
61+
options(W_matreex_edist = FALSE)
62+
```
63+
64+
# Uneven Harvest and Disturbance
65+
66+
```{r uneven_forest}
67+
68+
U_Forest <- forest(
69+
species = list(
70+
Fagus = species(IPM = Fagus_ipm, init_pop = def_initBA(30),
71+
harvest_fun = Uneven_harv, disturb_fun = disturb_fun,
72+
harv_lim = c(dth = 175, dha = 575, hmax = 1),
73+
disturb_coef = "sp")
74+
)
75+
)
76+
77+
disturb <- data.frame(type = "storm", intensity = 0.8, IsSurv = FALSE, t = 100)
78+
79+
U_Forest$species[[1]]$disturb_coef
80+
81+
Uneven_sim <- sim_deter_forest(
82+
U_Forest,
83+
tlim = 260, equil_time = 260, equil_dist = 10, equil_diff = 1,
84+
harvest = "Uneven", targetBA = 20,
85+
SurfEch = 0.03, disturbance = disturb,
86+
verbose = TRUE
87+
)
88+
89+
Uneven_sim %>%
90+
dplyr::filter(var %in% c("BAsp", "BAstand", "N"), ! equil) %>%
91+
ggplot(aes(x = time, y = value)) +
92+
facet_wrap(~ var, scales = "free_y") +
93+
geom_line(linewidth = .2) + geom_point(size = 0.4)
94+
95+
```
96+
97+
# Harvest and Disturbance competition
98+
99+
100+
```{r uneven_forest}
101+
load_all()
102+
E_Forest <- forest(
103+
species = list(
104+
Fagus = species(IPM = Fagus_ipm,
105+
init_pop = def_init_planting("Fagus_sylvatica"),
106+
harvest_fun = Even_harv, disturb_fun = disturb_fun,
107+
disturb_coef = "sp", rdi_coef = "sp") #,
108+
# Abies = species(IPM = Abies_ipm, init_pop = def_init_even,
109+
# harvest_fun = Even_harv, disturb_fun = disturb_fun,
110+
# disturb_coef = "sp", rdi_coef = "sp")
111+
),
112+
harv_rules = c(Pmax = 0.25, dBAmin = 3, freq = 5, alpha = 1)
113+
)
114+
115+
disturb <- data.frame(type = "storm", intensity = .5, IsSurv = FALSE, t = 150)
116+
117+
E_Forest$species[[1]]$IPM$info["delay"] <- 48
118+
as.numeric(E_Forest$species[[1]]$IPM$info["delay"])
119+
120+
Even_sim <- sim_deter_forest(
121+
E_Forest,
122+
tlim = 300, equil_time = 300, equil_dist = 10, equil_diff = 1,
123+
harvest = "Even", targetRDI = 0.6, targetKg = 0.7,
124+
final_harv = 120,
125+
disturbance = disturb,
126+
SurfEch = 0.03,
127+
verbose = TRUE
128+
)
129+
130+
Even_sim %>%
131+
# dplyr::filter(var %in% c("H", "N", "BAsp"), ! equil, time >90, time < 120) %>%
132+
dplyr::filter(var %in% c("BAsp", "H", "N"), ! equil) %>%
133+
ggplot(aes(x = time, y = value, color = species)) +
134+
facet_wrap(~ var, scales = "free_y") +
135+
geom_line(linewidth = .2) + geom_point(size = 0.4)
136+
137+
Even_sim %>%
138+
# dplyr::filter(var %in% c("H", "N", "BAsp"), ! equil, time >90, time < 120) %>%
139+
dplyr::filter(var %in% c("n", "h"), time %in% c(118:121, 165:175)) %>%
140+
ggplot(aes(x = size, y = value, color = species)) +
141+
facet_wrap(time ~ var, scales = "free_y") +
142+
geom_line(linewidth = .2) + geom_point(size = 0.4)
143+
144+
```
145+

dev/georges_Jasper.R

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Compute mean and sd of species dist for young stand for Jasper
2+
3+
#FUNDIV <- read.csv("data/NewFundiv/FUNDIV_tree.csv")
4+
FUNDIV <- read.csv("data/NewFundiv/FUNDIV_data.csv", sep = " ")
5+
6+
hist(FUNDIV$dbh2, breaks = 100)
7+
abline(v = 100, col = "red")
8+
9+
FUNDIV <- FUNDIV[FUNDIV$dbh2 >100, , ]
10+
FUNDIV <- FUNDIV[FUNDIV$treestatus %in% c(1,2), ]
11+
# 1 2 in growth alive
12+
library(dplyr)
13+
library(tidyr)
14+
library(Hmisc)
15+
16+
# Compute dominant species in ba
17+
fun_dom_sp <- function (sp,ba, Nha){
18+
res <- data.frame(sp = sp, ba_ha2 = ba, Nha = Nha) %>% group_by(sp) %>% summarise(BA = sum(ba_ha2), N = sum(Nha)) %>%
19+
arrange(desc(BA)) %>%ungroup() %>% mutate(BA_per = BA/sum(BA))
20+
return((res[1,]))
21+
}
22+
23+
df <- FUNDIV %>% group_by(plotcode) %>%
24+
summarise(country = unique(country),
25+
MQD_log = sum(log(dbh2)*Nha2)/sum(Nha2),
26+
MQD_log_sd = sqrt(wtd.var(log(dbh2), Nha2)),
27+
domsp = list(fun_dom_sp(sp=species, ba = Nha2 *(dbh2/1000)^2/4*pi, Nha = Nha2)))%>%
28+
unnest_wider(domsp)
29+
30+
sp_sel <- names(sort(table(df$sp), decreasing = TRUE)[1:45])
31+
c("Pinus sylvestris", "Picea abies", "Fagus sylvatica", "Quercus ilex",
32+
"Pinus pinaster", "Pinus halepensis", "Quercus robur", "Quercus petraea",
33+
"Pinus nigra", "Betula sp")
34+
res <- df %>% filter(sp %in% sp_sel ,
35+
BA_per >0.9,
36+
MQD_log < log(120))%>%
37+
group_by(sp) %>% summarise(MQD_log = mean(MQD_log),
38+
MQD_log_sd = mean(MQD_log_sd),
39+
Nha = mean(N),
40+
BA = mean(BA))
41+
42+
res <- recrut[, -1]
43+
names(res) <- c("sp", "MQD_log", "MQD_log_sd", "Nha", "BAha")
44+
45+
# Plot of the simulated distribution
46+
par(mfrow = c(2,5))
47+
for (i in 1:10){
48+
hist(exp(rnorm(10000, mean = res$MQD_log[i], sd = res$MQD_log_sd[i])),
49+
main = res$sp[i], breaks = 40, probability= TRUE, ylim = c(0, 1))
50+
# size mesh for tree size
51+
mesh <- 100:150
52+
# compute relative probability density for each mesh based on lognormal proba
53+
prob_dens <- dnorm(log(mesh), mean = res$MQD_log[i], sd = res$MQD_log_sd[i])/
54+
(sum(dnorm(log(mesh), mean = res$MQD_log[i], sd = res$MQD_log_sd[i])))
55+
lines(mesh, prob_dens, col = "blue")
56+
abline(v = 99, col = "red")
57+
# compute basal area from size dist and mean density and compare to mean basal area in data
58+
print(res$sp[i])
59+
print(sum(mesh^2/(4*1000*1000) *pi * prob_dens *res$Nha[i]))
60+
print(res$BA[i])
61+
}
62+
names(res) <- c("species", "mean_DBH_log", "sd_DBH_log", "Nha", "BAha")
63+
64+
write.csv(res, file = "ParamSizeDistYoungStand.csv")
65+
#How to introduce this new cohort ? Start after the time lag of the species? As plantation shorter than the full time lag ? By how much ?10 years?
66+
# A given percentage of teh time lag ? 50 % ?
67+
68+
69+
#Not possible to get the structure in the lag Y years before by inverting the IPM because the IPM is non invertible (its determinant is zero)
70+
71+
# writing a function to use these parameters
72+
matreex::distrib_planting
73+
load_all()
74+
data("fit_Picea_abies")
75+
data("climate_species")
76+
climate <- subset(climate_species, N == 2 & sp == "Picea_abies", select = -sp)
77+
78+
Picea_ipm <- make_IPM(
79+
species = "Picea_abies",
80+
climate = climate,
81+
fit = fit_Picea_abies,
82+
clim_lab = "optimum clim",
83+
mesh = c(m = 700, L = 90, U = get_maxdbh(fit_Picea_abies) * 1.1),
84+
BA = 0:60, # Default values are 0:200, smaller values speed up this vignette.
85+
verbose = TRUE
86+
)
87+
88+
fun <- def_init_planting("Picea_abies")
89+
fun(Picea_ipm$mesh, 0.03)

man/def_initBA.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/def_init_even.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/def_init_k.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)