CreateSpacetimeFolds: Create Space-time Folds

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/CreateSpacetimeFolds.R

Description

Create spatial, temporal or spatio-temporal Folds for cross validation

Usage

1
2
3
4
5
6
7
8
CreateSpacetimeFolds(
  x,
  spacevar = NA,
  timevar = NA,
  k = 10,
  class = NA,
  seed = sample(1:1000, 1)
)

Arguments

x

data.frame containing spatio-temporal data

spacevar

Character indicating which column of x identifies the spatial units (e.g. ID of weather stations)

timevar

Character indicating which column of x identifies the temporal units (e.g. the day of the year)

k

numeric. Number of folds. If spacevar or timevar is NA and a leave one location out or leave one time step out cv should be performed, set k to the number of unique spatial or temporal units.

class

Character indicating which column of x identifies a class unit (e.g. land cover)

seed

numeric. See ?seed

Details

Using "class" is helpful in the case that data are clustered in space and are categorical. E.g This is the case for land cover classifications when training data come as training polygons. In this case the data should be split in a way that entire polygons are held back (spacevar="polygonID") but at the same time the distribution of classes should be similar in each fold (class="LUC").

Value

A list that contains a list for model training and a list for model validation that can directly be used as "index" and "indexOut" in caret's trainControl function

Note

Standard k-fold cross-validation can lead to considerable misinterpretation in spatial-temporal modelling tasks. This function can be used to prepare a Leave-Location-Out, Leave-Time-Out or Leave-Location-and-Time-Out cross-validation as target-oriented validation strategies for spatial-temporal prediction tasks. See Meyer et al. (2018) for further information.

Author(s)

Hanna Meyer

References

Meyer, H., Reudenbach, C., Hengl, T., Katurji, M., Nauß, T. (2018): Improving performance of spatio-temporal machine learning models using forward feature selection and target-oriented validation. Environmental Modelling & Software 101: 1-9.

See Also

trainControl,ffs

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dat <- get(load(system.file("extdata","Cookfarm.RData",package="CAST")))
### Prepare for 10-fold Leave-Location-and-Time-Out cross validation
indices <- CreateSpacetimeFolds(dat,"SOURCEID","Date")
str(indices)
### Prepare for 10-fold Leave-Location-Out cross validation
indices <- CreateSpacetimeFolds(dat,spacevar="SOURCEID")
str(indices)
### Prepare for leave-One-Location-Out cross validation
indices <- CreateSpacetimeFolds(dat,spacevar="SOURCEID",
k=length(unique(dat$SOURCEID)))
str(indices)

Example output

List of 2
 $ index   :List of 10
  ..$ : int [1:172541] 61 62 63 64 65 66 67 68 69 70 ...
  ..$ : int [1:184118] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:182721] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:177967] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:184336] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:172120] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:176963] 6 7 8 9 10 11 12 13 14 15 ...
  ..$ : int [1:176744] 1 2 3 4 5 11 12 13 14 15 ...
  ..$ : int [1:175671] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:172412] 1 2 3 4 5 6 7 8 9 10 ...
 $ indexOut:List of 10
  ..$ : int [1:2706] 36 37 38 39 40 56 57 58 59 60 ...
  ..$ : int [1:1462] 758 766 773 781 788 1098 1110 1121 1133 1144 ...
  ..$ : int [1:1627] 61 73 84 96 288 300 311 323 345 357 ...
  ..$ : int [1:2151] 1388 1399 1411 1423 1434 2789 2800 2812 2824 2835 ...
  ..$ : int [1:1461] 1555 1578 1590 1601 2500 2523 2535 2546 3640 3663 ...
  ..$ : int [1:2729] 1214 1218 1225 1229 1237 1241 1249 1253 1260 1264 ...
  ..$ : int [1:2238] 183 194 206 218 229 411 422 434 446 457 ...
  ..$ : int [1:2225] 119 131 142 153 164 232 244 255 267 278 ...
  ..$ : int [1:2339] 580 587 595 603 727 734 742 750 936 947 ...
  ..$ : int [1:2735] 687 688 694 695 702 703 710 711 717 718 ...
List of 2
 $ index   :List of 10
  ..$ : int [1:204375] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:195583] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:193391] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:193951] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:195241] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:193639] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:199715] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:198276] 61 62 63 64 65 66 67 68 69 70 ...
  ..$ : int [1:199741] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:199248] 1 2 3 4 5 6 7 8 9 10 ...
 $ indexOut:List of 10
  ..$ : int [1:14865] 67 78 90 102 113 124 135 146 158 169 ...
  ..$ : int [1:23657] 18518 18522 18531 18535 18544 18548 18557 18561 18570 18574 ...
  ..$ : int [1:25849] 66 77 89 101 112 123 134 145 157 168 ...
  ..$ : int [1:25289] 70 81 93 105 116 127 138 149 161 172 ...
  ..$ : int [1:23999] 65 72 76 83 88 95 100 107 111 122 ...
  ..$ : int [1:25601] 61 69 73 80 84 92 96 104 115 118 ...
  ..$ : int [1:19525] 63 68 75 79 86 91 98 103 109 114 ...
  ..$ : int [1:20964] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:19499] 64 87 99 110 121 144 155 166 177 200 ...
  ..$ : int [1:19992] 62 74 85 97 108 119 131 142 153 164 ...
List of 2
 $ index   :List of 42
  ..$ : int [1:212280] 61 62 63 64 65 66 67 68 69 70 ...
  ..$ : int [1:213423] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213141] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214298] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213327] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:212680] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213453] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:212953] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214291] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:212537] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:211470] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:212754] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:215066] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:215344] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214579] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214567] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214176] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214048] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213796] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:216081] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:215100] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214259] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213841] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214534] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213535] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213593] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214689] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213974] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213479] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214334] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:215005] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213794] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:215335] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:212692] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214403] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214993] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213650] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214278] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:216264] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214977] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:214224] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:213623] 1 2 3 4 5 6 7 8 9 10 ...
 $ indexOut:List of 42
  ..$ : int [1:6960] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:5817] 61 73 84 96 118 130 141 152 174 186 ...
  ..$ : int [1:6099] 62 74 85 97 108 119 131 142 153 164 ...
  ..$ : int [1:4942] 63 75 86 98 109 120 132 143 154 165 ...
  ..$ : int [1:5913] 64 87 99 110 121 144 155 166 177 200 ...
  ..$ : int [1:6560] 65 76 88 100 111 122 133 156 167 178 ...
  ..$ : int [1:5787] 66 77 89 101 112 123 134 145 157 168 ...
  ..$ : int [1:6287] 67 78 90 102 113 124 135 146 158 169 ...
  ..$ : int [1:4949] 68 79 91 103 114 125 136 147 159 170 ...
  ..$ : int [1:6703] 69 80 92 104 115 126 137 148 160 171 ...
  ..$ : int [1:7770] 70 81 93 105 116 127 138 149 161 172 ...
  ..$ : int [1:6486] 72 83 95 107 129 140 151 163 185 196 ...
  ..$ : int [1:4174] 18518 18531 18544 18557 18570 18583 18608 18633 18657 18682 ...
  ..$ : int [1:3896] 18519 18532 18545 18558 18571 18584 18609 18634 18658 18683 ...
  ..$ : int [1:4661] 18520 18533 18546 18559 18572 18585 18610 18635 18659 18684 ...
  ..$ : int [1:4673] 18521 18534 18547 18560 18573 18586 18611 18636 18660 18685 ...
  ..$ : int [1:5064] 18522 18535 18548 18561 18574 18587 18612 18637 18661 18686 ...
  ..$ : int [1:5192] 18523 18536 18549 18562 18575 18588 18613 18638 18662 18687 ...
  ..$ : int [1:5444] 18524 18537 18550 18563 18576 18593 18618 18643 18667 18692 ...
  ..$ : int [1:3159] 18525 18538 18551 18564 18577 18594 18619 18644 18668 18693 ...
  ..$ : int [1:4140] 18526 18539 18552 18565 18578 18595 18620 18645 18669 18694 ...
  ..$ : int [1:4981] 18527 18540 18553 18566 18579 18597 18622 18647 18671 18696 ...
  ..$ : int [1:5399] 18528 18541 18554 18567 18580 18598 18623 18648 18672 18697 ...
  ..$ : int [1:4706] 18529 18542 18555 18568 18581 18599 18624 18649 18673 18698 ...
  ..$ : int [1:5705] 18530 18543 18556 18569 18582 18601 18626 18651 18675 18700 ...
  ..$ : int [1:5647] 18589 18614 18639 18663 18688 18713 18739 18765 18791 18817 ...
  ..$ : int [1:4551] 18590 18615 18640 18664 18689 18714 18740 18766 18792 18818 ...
  ..$ : int [1:5266] 18591 18616 18641 18665 18690 18715 18741 18767 18793 18819 ...
  ..$ : int [1:5761] 18592 18617 18642 18666 18691 18716 18742 18768 18794 18820 ...
  ..$ : int [1:4906] 18596 18621 18646 18670 18695 18720 18746 18772 18798 18824 ...
  ..$ : int [1:4235] 18600 18625 18650 18674 18699 18724 18750 18776 18802 18828 ...
  ..$ : int [1:5446] 18602 18627 18652 18676 18701 18726 18752 18778 18804 18830 ...
  ..$ : int [1:3905] 18603 18628 18653 18677 18702 18728 18754 18780 18806 18832 ...
  ..$ : int [1:6548] 18604 18629 18654 18678 18703 18729 18755 18781 18807 18833 ...
  ..$ : int [1:4837] 18605 18630 18655 18679 18704 18730 18756 18782 18808 18834 ...
  ..$ : int [1:4247] 18606 18631 18656 18680 18705 18731 18757 18783 18809 18835 ...
  ..$ : int [1:5590] 18607 18632 18681 18706 18732 18758 18784 18810 18836 18864 ...
  ..$ : int [1:4962] 18837 18865 18891 18919 18946 18973 19005 19036 19069 19101 ...
  ..$ : int [1:2976] 18839 18867 18893 18921 18948 18975 19007 19038 19071 19103 ...
  ..$ : int [1:4263] 18844 18872 18898 18926 18953 18981 19013 19044 19077 19109 ...
  ..$ : int [1:5016] 18846 18874 18900 18928 18955 18983 19015 19046 19079 19111 ...
  ..$ : int [1:5617] 18859 18885 18913 18940 18967 18999 19029 19062 19094 19126 ...

CAST documentation built on April 7, 2021, 5:09 p.m.