Skip to content

Instantly share code, notes, and snippets.

@SimonGoring
Created July 29, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonGoring/a085efed1455dfdd0e2d to your computer and use it in GitHub Desktop.
Save SimonGoring/a085efed1455dfdd0e2d to your computer and use it in GitHub Desktop.
This is a chunk of my RMarkdown file that's having a problem.
```{r figure1, fig.width=6, fig.height=5, echo = FALSE, warning = FALSE, message=FALSE}
# This is the chunk where 'climate_values' is created (it's the one causing errors)
land_use$class[land_use$class == 'Water'] <- NA
land_use_plot <- natural[[2]] +
geom_tile(data = land_use, aes(x = x, y = y, fill = class)) +
scale_fill_manual(values = c('#ff7f00', '#e41a1c', '#33a02c'))
source('R/figures/climate_ts.R')
climate_values <- climate_ts(unit.raster, pls_data, climate$now, climate$then)
# climate_values is a list: list(modern = modern, past = past, climate = climate_ann, plot = clim_plot)
grid.arrange(climate_values$plot, land_use_plot, nrow=1, widths = c(3, 4))
```
Then there's a bunch of text. . .
**Figure 1.** *(a-e) Climate change in the Upper Midwest over the last two centuries using PRISM data and North American Drought Atlas PDSI reconstructions. Low temperatures in the 1970s result in lower T_max_ during the modern era, but climate change has resulted in higher T_min_ values than the early-century normals, along with increasing A_ppt_. PDSI shows strong coherence with A_ppt_ in this region. (f) Patterns of land use change in the upper Midwestern United States. Data from the NLCD [@jin2013comprehensive].*
The interaction of land use change and climatic change has been of interest to conservation managers and scientists [e.g., @pyke2004habitat] since managed natural areas often represent a biased selection from the available environmental space [@scott2001nature]. Pyke [-@pyke2004habitat] lays out a useful framework for assessing the interaction of climate and land use change. By deliniating three tpyes of interaction, compunding, confounding and contradicting, it is possible to understand both the nature of the interaction and the potential effect on species resiliance and response to broader regional change.
Compounding effects occur when both climatic change and land use change are biased in the same direction, *e.g.*, warming climate is compounded by land use change that is biased towards warmer environments. In the case of warming a species may lose climatic suitability along the warmest margins of its range at the same time as land use pressure increases, hastening decline in response to warming. Confounding effects may cause little overall effect on species distributions since in this case the land use pressure falls within the central distribution for the environmental variable, they may increase the variability of predictive models, but do little to the overall envelope. Contradicting effects result in a narrower climate niche for taxa, where warming forces the distribution mean higher as populations in the warmer tail of the distribution experience greater environmental pressure, and, secondarily environmentally biased land use pressure in the cooler end of the tail excludes the taxa from those regions.
In the midwestern United States we see a strong gradient of temperature and precipitation that results in two major ecotones, one from prairie/savanna to closed forest, in an approixmately west to east direction, and one from conifer dominated, sub-boreal forests to decidious forest in a north to south direction. Goring et al [@goring2015preset] have shown that land use change has significantly changed the strength and structure of this ecotone, but did not examine how these shifts might affect the relationships between climate and distribution for individual taxa. Given that much of the land use change within the region occurs in the south, where agricultural conversion has largely eliminated open forests, we might expect to see that species with more southerly distributions would show greater impacts of land use change on their distributions in climate space, assuming no shift in regional climate. Conversely, species in the north should show little change if we expect that pre-settlement trees show similar regeneration patterns (with respect to simple presence/absence) following widespread logging.
Interaction between environmental gradients and land use patterns vary by region and environmental variable. We might expect that species in the north experience a confounding land use effect with respect to temperature, but, with respect to precipitation, particularly toward the forest-prairie margin, where land use conversion continues [@wright2013recent] we would expect this effect to be compounding. For southern tree species land use conversion is primarily, and has primarily occured along the southern margins of the region, parallel to temperature and precipitation gradients. This should result in contradicting land use effects for all taxa with distributions in the region, resulting in narrower species distributions due to the twin effects of land use change and regional climate change.
Critically, given the importance of modern distributional datasets for predicting species range shifts, contradicting land use effects may result in the impression of narrower climatic tolerances. For species that reach their northern or western range limits in the upper Midwestern United States (e.g., hemlock, beech, &tc.) this may be critical, since a narrowing of the apparent environmental envolope may result in underprediction of species resiliance to climate change.
We use gridded climate data products and estimates of pre-settlement vegetation to develop climate-vegetation relationships for 15 major tree taxa in the upper Midwestern United States to understand how climate and land use change have interacted since Euro-American settlement to affect species distributions in the region, and to examine the possible implications of these shifts to future estimates of species resillience to climate change across the region.
Methods
===============
We pair pre-settlement vegetation data from the Public Land Survey System aggregated to a 64km^2^ grid [@goring2015preset] with annually resolved climate data for the region.
**Paragraph about data scale between PLS and FIA**
**Paragraph about FIA/PLS absence not really Absence**
The analysis follows a blocked sampling method. We generate probability density functions for actual PLSS presence, by taxon, at the settlement era and for the same PLS taxa (and point locations) in the modern era. We also generate climate data for the FIA presence, and we generate climate space for modern land use. In this way we can test how much of the change in shape is the result of changing land use vs changing climate since the early part of the century.
We also develop estimates of the modern climate space that taxa would occupy if they remained in place, with only climate changing, and vice versa (need better explanation here).
Results
---------------
The choice of a normal for the domain is potentially problematic. The time transgressive nature of the PLS Survey means that some of the PLS data comes from records sampled in the early 1800s, while other come from the early 1900s. Regardless, throughout this period climate variability exists (Figure 1d), and trees that were on the landscape from the 1830s to the 1880s are not expected to have been completely extirpated by annual scale climate variability in that same time period. As such the use of a 'pre-settlement' climate normal from 1895-1925 is likely a reasonable choice [ADD JULY temperatures here]. **Talk about modern climate normal & historical climate normal make it clear that this is the best we can do, and that it's good.**
This is where the problem happens:
`r clim_change <- climate_values$modern$value - climate_values$past$value `
Mean temperatures in the region show an average increase of `r round(clim_change[3], 2)`^o^C since the 1895-1925 normal. Annual precipitation has increased slightly (`r round(clim_change[1], 0)`mm), and maximum annual temeprature has declined slightly (`r round(clim_change[2], 2)`^o^C)). The most significant change has been in annual minimum temperatures, which have increased by `r round(clim_change[4], 1)` since the 1895-1925 normal. This broad scale increase in winter temperatures is widely accepted, and results in an overall shift in the winter 'climate space' for the region.
The Public Lands data is spatially extensive, sampling occured across the region in a regular pattern, while the FIA is limited to regions with forest cover. As such, the extent of points is not overlapping. Given the extensive use of FIA data in generating and estimating climate responses of tree taxa and forest types to changing climate, the use of the FIA data as an estimate for tree species distributions is acceptable fo estimating shifts in climate space, and attributing the loss or gain of climate niche space due to land use and climate variability and change during the 20^th^ century.
@SimonGoring
Copy link
Author

When I knit I end with this:

label: figure1 (with options) 
List of 5
 $ fig.width : num 6
 $ fig.height: num 5
 $ echo      : logi FALSE
 $ warning   : logi FALSE
 $ message   : logi FALSE

  |..............................                                   |  47%
   inline R code fragments


Quitting from lines 102-139 (Composition_Ecology.Rmd) 
Error in eval(expr, envir, enclos) : object 'climate_values' not found
Calls: <Anonymous> ... in_dir -> inline_exec -> withVisible -> eval -> eval
Execution halted

@SimonGoring
Copy link
Author

Obv. the line numbers don't line up here, but 102 is the end of the chunk and the start of the markdown text.

@kbroman
Copy link

kbroman commented Jul 29, 2015

Very weird.

I've never done assignment within in-line code; maybe move that to a regular chunk?

Then I'd start doing some experiments to figure out what's happening:

  • add another code chunk that explicitly defines climate_values
  • add another code chunk that tests, in a different way, whether the figure1 chunk had created the climate_values object.

@SimonGoring
Copy link
Author

I'm leaving this up, even through I've solved the problem. The problem seems to have come from an error thrown in the chunk figure1, not in the inline text. For some reason that error didn't cause knitr to stop, making the error message less helpful.

Fixing code in the function climate_ts solved the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment