I have found a problem with the dcc()
function where it can give spurious results if the months in the climate
input are not ordered "correctly". For example in a data.frame
, the function assumes that months are ordered like this: 1,2,3,4,5,6,7,8,9,10,11,12
, even if they are not. See the example below where I have reordered months (without changing the data), but the function gives a spurious result.
This seems like a simple fix. All the function needs is something equivalent to this before running correlations:
climate <- climate[order(climate$year, climate$months, decreasing = F),]
Although perhaps a better fix would involve lookup tables or some other matching or indexing.
I'm not sure if this problem extends beyond dcc()
, but it seems worth a look.
# Example from dcc() function help page
dc_resp <- dcc(muc_spruce, muc_clim)
plot(dc_resp) # expected result
# Change order of climate data
muc_clim_wrong <- muc_clim[order(muc_clim$month, decreasing = T),]
# Note that the data is still correct:
# months are associated with the correct respective values
aggregate(temp ~ month, data = muc_clim, mean)
aggregate(temp ~ month, data = muc_clim_wrong, mean)
# Run the function again
dc_resp_wrong <- dcc(muc_spruce, muc_clim_wrong)
# Very different results when months are not ordered as expected.
# Plot labels stay the same.
plot(dc_resp_wrong) # different from expected result
# Simply reordering by month doesn't fix the problem:
muc_clim_still_wrong <- muc_clim_wrong[order(muc_clim_wrong$month, decreasing = F),]
dc_resp_still_wrong <- dcc(muc_spruce, muc_clim_still_wrong)
plot(dc_resp_still_wrong)
# This does work though:
muc_clim_right <- muc_clim_wrong[order(muc_clim_wrong$year, muc_clim_wrong$month, decreasing = F),]
dc_resp_right <- dcc(muc_spruce, muc_clim_right)
plot(dc_resp_right)