Introduction to Machine Learning with RandomForest

Install Packages:

install.packages("randomForest")
install.packages("tidyverse")
install.packages("GGally")

Load Libraries:

library(randomForest)
library(tidyverse)
library(GGally)

Boosted regression trees (BRT) represent a versatile machine learning technique applicable to both classification and regression tasks. This approach facilitates the assessment of the relative significance of numerous variables associated with a target response variable. In this workshop, our focus will be on utilizing BRT to develop a model for monthly methane fluxes originating from natural ecosystems. We’ll leverage climate and moisture conditions within these ecosystems to enhance predictive accuracy and understanding.

Read in the data:

load('data/RANDOMFOREST_DATASET.RDATA' )

Our ultimate interest is in predicting monthly methane fluxes using both dynamic and static attribute of ecosystems. Before we start modeling with the data, it is a good practice to first visualize the variables. The ggpairs() function from the GGally package is a useful tool that visualizes the distribution and correlation between variables:

ggpairs(fluxnet, columns = c(3:7, 12:13))

Next we need to divide the data into testing (20%) and training (80%) sets in a reproducible way:

set.seed(111) # set the randomnumber generator

#create ID column
fluxnet$id <- 1:nrow(fluxnet)

#use 80% of dataset as training set and 30% as test set 
train <- fluxnet %>% dplyr::sample_frac(0.80)
test  <- dplyr::anti_join(fluxnet, train, by = 'id')

We will use the randomForest() function to predict monthly natural methane efflux using several variables in the dataset. A few other key statements to use in the randomForest() function are:

  1. keep.forest = T: This will save the random forest output, which will be helpful in summarizing the results.
  2. importance = TRUE: This will assess the importance of each of the predictors, essential output in random forests.
  3. mtry = 1: This tells the function to randomly sample one variable at each split in the random forest. For applications in regression, the default value is the number of predictor variables divided by three (and rounded down). In the modeling, several small samples of the entire data set are taken. Any observations that are not taken are called “out-of-bag” samples.
  4. ntree = 500: This tells the function to grow 500 trees. Generally, a larger number of trees will produce more stable estimates. However, increasing the number of trees needs to be done with consideration of time and memory issues when dealing with large data sets.

Our response variable in the random forests model is FCH4_F_gC and predictors are P_F, TA_F, VPD_F, IGBP, NDVI, and EVI. We will only explore a few of these variables below:

FCH4_F_gC.rf <- randomForest(FCH4_F_gC ~ P_F + TA_F + VPD_F ,
                        data = train,
                        keep.forest = T,
                        importance = TRUE, 
                        mtry = 1,
                        ntree = 500,
                        keep.inbag=TRUE)
FCH4_F_gC.rf

Call:
 randomForest(formula = FCH4_F_gC ~ P_F + TA_F + VPD_F, data = train,      keep.forest = T, importance = TRUE, mtry = 1, ntree = 500,      keep.inbag = TRUE) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 1

          Mean of squared residuals: 4.887725
                    % Var explained: 22.83

Note the mean of squared residuals and the percent variation explained (analogous to R-squared) provided in the output.

Visualize the out-of-bag error rates of the random forests models using the plot() function. In this application, although we specified 500 trees, the out-of-bag error generally stabilizes after 100 trees:

plot(FCH4_F_gC.rf)

Some of the most helpful output in random forests is the importance of each of the predictor variables. The importance score is calculated by evaluating the regression tree with and without that variable. When evaluating the regression tree, the mean square error (MSE) will go up, down, or stay the same.

If the percent increase in MSE after removing the variable is large, it indicates an important variable. If the percent increase in MSE after removing the variable is small, it’s less important.

The importance() function prints the importance scores for each variable and the varImpPlot() function plots them:

importance(FCH4_F_gC.rf)
         %IncMSE IncNodePurity
P_F   11.7269452      2762.568
TA_F  29.1106456      4454.298
VPD_F -0.7084245      3396.256
varImpPlot(FCH4_F_gC.rf)

Another aspect of model evaluation is comparing predictions. Although random forests models are often considered a “black box” method because their results are not easily interpreted, the predict() function provides predictions of total tree mass:

train$PRED.TPVPD <- predict(FCH4_F_gC.rf, train)
train$PRED.TPVPD
           1            2            3            4            5            6 
 0.350966664  0.429278864  0.313910434  0.418912296  1.106883276  2.202318614 
           7            8            9           10           11           12 
 3.022301696  3.281477224  1.982611915  0.710828667  0.513731178  0.440529651 
          13           14           15           16           17           18 
 0.423833010  0.279860587  0.371487016  0.498307150  0.936792763  2.202429003 
          19           20           21           22           23           24 
 0.584097127  3.814697537  1.035535080  3.480349355  1.767388586  0.732192556 
          25           26           27           28           29           30 
 0.487254095  0.411031890  0.389717998  0.340486452  0.337745909  0.662268060 
          31           32           33           34           35           36 
 0.846607274  1.057445926  2.403118584  0.572516189  3.698548702  2.835909192 
          37           38           39           40           41           42 
 0.802947684  1.143423013  0.676826308  0.641043739  0.488707123  0.379251231 
          43           44           45           46           47           48 
 0.315522422  0.261512975  0.690590324  0.487961155  2.953092995  1.578643183 
          49           50           51           52           53           54 
 2.130515801  1.721572006  2.736948169  0.776934319  1.640462055  3.153600168 
          55           56           57           58           59           60 
 7.646926404  6.835070552  1.037728598  0.374152315  1.049627742  2.081257999 
          61           62           63           64           65           66 
 1.124287950  0.604855671  0.670482666  0.761253054  0.206999823  0.433200877 
          67           68           69           70           71           72 
 0.752380451  0.426073762  0.222362734  1.012824599  0.262397242  1.742353734 
          73           74           75           76           77           78 
 1.635003299  0.726266987  0.387179570  1.138539924  0.735700517  0.496176925 
          79           80           81           82           83           84 
 0.585429940  1.448738513  0.773433394  0.219944262  0.905490335  0.965036433 
          85           86           87           88           89           90 
 2.173433298  0.690692175  2.097052031  1.578422868  2.097958541  3.498357683 
          91           92           93           94           95           96 
 1.790640635  6.595011399  0.932766638  2.790858891  4.256334356  1.308749512 
          97           98           99          100          101          102 
 0.294418840  1.315675200  0.635219978  0.301323511  0.443618683  0.963547668 
         103          104          105          106          107          108 
 0.213003224  1.017388068  0.654954329  0.730847957  0.293852780  0.404445256 
         109          110          111          112          113          114 
 0.174803398  0.183022055  0.540337242  0.372990615  0.091798887  0.569426291 
         115          116          117          118          119          120 
 0.633679309  0.132305375  0.085387653  0.781615646  1.117640973  0.112640327 
         121          122          123          124          125          126 
 1.837179391  1.639796888  0.086019047  0.278281849  0.365172124  2.233347544 
         127          128          129          130          131          132 
 0.816182644  1.508493142  0.303020770  0.708380725  0.642802520  0.916800286 
         133          134          135          136          137          138 
 5.721636509  1.738748689  0.474685634  2.749514872  2.077956662  1.254022333 
         139          140          141          142          143          144 
 5.706288209  2.738208415  0.590629677  1.118304071  2.716054553  5.099980652 
         145          146          147          148          149          150 
 3.983072173  0.965444502  1.659923242  0.912270359  7.016639798  1.992233306 
         151          152          153          154          155          156 
 7.064628027  0.580233628  7.624291639  0.511706938  0.523403391  7.311759127 
         157          158          159          160          161          162 
 0.413081693  2.615485887  1.737803176  4.041238549  1.766462799  0.378158206 
         163          164          165          166          167          168 
 1.095346427  1.756824110  0.796378218  5.051853990  5.954580676  3.474148805 
         169          170          171          172          173          174 
 2.565140713  0.867434888  0.538638973  1.696448825  0.265605511  0.711388620 
         175          176          177          178          179          180 
 0.332579885  1.152890181  0.161130895  0.123131558  0.734373503  1.136376349 
         181          182          183          184          185          186 
 0.320969032  1.663430779  3.110695368  0.242585633  0.505072358  1.140161420 
         187          188          189          190          191          192 
 0.851390131  1.071771476  1.839179031  0.637214355  0.202589105  0.274153351 
         193          194          195          196          197          198 
 0.109863304  0.195861100  0.940964660  0.975494373  0.167648474  0.359387404 
         199          200          201          202          203          204 
 0.180352975  2.845530542  0.071864543  0.280249651  0.440519431  0.402323824 
         205          206          207          208          209          210 
 0.895949134  0.632782960  0.714863603  0.257169462  3.221095723  1.169616608 
         211          212          213          214          215          216 
 0.124731449  0.195806026  0.542832946  0.940700661  0.960319390  0.255120260 
         217          218          219          220          221          222 
 0.406463453  0.322686002  0.868392469  0.517672473  1.228435930  0.248966587 
         223          224          225          226          227          228 
 0.392391140  0.374838512  0.234492682  1.142514595  0.966696768  0.415182137 
         229          230          231          232          233          234 
 0.453868582  1.266918517  0.904053662  3.482301871  1.412714264  0.716085175 
         235          236          237          238          239          240 
 0.547671777  2.235410240  0.739637308  0.361864774  1.598877413  0.111295017 
         241          242          243          244          245          246 
 0.678749343  1.824585623  0.132786405  0.253973400  3.244061260  0.619423508 
         247          248          249          250          251          252 
 2.696579716  0.381349587  1.304924370  0.841318717  1.404538529  5.074112507 
         253          254          255          256          257          258 
 1.091185551  2.433840657  0.835469160  0.390162136  3.453057420  0.892346779 
         259          260          261          262          263          264 
 1.256747363  1.768921752  5.559813717  3.642332054  6.916058334  0.345531128 
         265          266          267          268          269          270 
 3.609315795  3.458312122  5.411483135  0.478304689  0.384067687  1.437609539 
         271          272          273          274          275          276 
 1.646644444  0.880818558  1.837048719  2.144814211  5.179255187  1.097206418 
         277          278          279          280          281          282 
 1.038038519  2.968948482  7.818837967  3.256186938  2.412005486  5.419321707 
         283          284          285          286          287          288 
 0.813317122  8.317115079  2.601755469  0.843429860  1.612596659  2.860754089 
         289          290          291          292          293          294 
 0.816462385  2.172526008  1.340129398  5.117156056  1.275575291  4.462547926 
         295          296          297          298          299          300 
 1.436489958  5.341489754  6.841656521  2.469832570  0.441526857  1.610415035 
         301          302          303          304          305          306 
 2.125540705  1.595491350  2.847664153  6.501367627  1.109501291  6.475184329 
         307          308          309          310          311          312 
 1.007149232  0.280221096  0.561981959  3.314203089  6.628252055  0.831186784 
         313          314          315          316          317          318 
 1.215601255  5.237720926  1.883128960  2.408583467  1.325263889  3.733852759 
         319          320          321          322          323          324 
 5.463872633  0.274794480  5.627900247  1.334480215  1.615528957  1.759371603 
         325          326          327          328          329          330 
 4.213020843  0.240711557  0.140864700  0.230417115  0.481527167  4.990077329 
         331          332          333          334          335          336 
 1.408444343  0.717380467  0.480762063  0.522399253  0.599568414  0.261180604 
         337          338          339          340          341          342 
 0.650229297  1.213234689  2.678496029  0.088922976  0.403422319  0.170017229 
         343          344          345          346          347          348 
 1.867639035  1.375723662  0.238104475  1.343953452  0.585615214  0.431887055 
         349          350          351          352          353          354 
 0.419542218  0.327553092  0.854287088  0.708303466  0.642098880  0.252738257 
         355          356          357          358          359          360 
 0.719312298  1.109625844  0.116976115  1.097257013  0.276706131  0.553577086 
         361          362          363          364          365          366 
 2.161842623  0.113232453  2.089783482  0.236194592  2.564091189  0.189153563 
         367          368          369          370          371          372 
 0.207067256  0.437531195  4.302379780  0.315423479  0.729131454  0.373449287 
         373          374          375          376          377          378 
 0.081579816  0.216232553  0.605548923  0.233032154  0.488667389  0.212117520 
         379          380          381          382          383          384 
 2.369578546  0.100417073  0.272551495  0.229942998  0.404410682  0.812587221 
         385          386          387          388          389          390 
 1.227797302  4.171470801  0.517796566  0.169644735  0.232706420  0.190779549 
         391          392          393          394          395          396 
 3.440407131  0.787629147  0.438585080  0.334631760  2.678008905  0.448884859 
         397          398          399          400          401          402 
 0.210995605  0.277910907  2.012415825  0.211872962  0.567612565  2.551500208 
         403          404          405          406          407          408 
 3.272335602  1.400061696  1.167298354  0.345972295  1.093672968  0.206106718 
         409          410          411          412          413          414 
 1.669648952  0.131664918  2.604217697  3.192156989  0.428175081  0.285481063 
         415          416          417          418          419          420 
 0.338054229  0.753645508  0.506670906  0.717317100  0.367000573  0.738708482 
         421          422          423          424          425          426 
 1.094189654  3.160629273  3.305837721  5.706801716  1.592265430  2.811905665 
         427          428          429          430          431          432 
 0.500651788  0.594767532  0.583573842  0.221870210  2.855619593  3.954433601 
         433          434          435          436          437          438 
 1.032011262  0.857245197  0.728714314  1.836422539  1.296894125  1.700360179 
         439          440          441          442          443          444 
 1.704780292  1.304512532  4.609975803  3.656038932  4.537352098  2.002747940 
         445          446          447          448          449          450 
 3.057017886  0.402028949  3.822736954  0.757225344  1.343776950  0.797768429 
         451          452          453          454          455          456 
 1.970830947  0.883421710  0.761662008  0.860911293  6.232388290  0.411650784 
         457          458          459          460          461          462 
 7.391289267  5.052575707  2.897728615  1.459895408  1.839558696  2.225035682 
         463          464          465          466          467          468 
 2.069839957  6.794525818  0.895532118  2.372073435  0.695066681  1.393262267 
         469          470          471          472          473          474 
 1.409907929  0.663635225  1.131757609  1.904746198  1.773330010  3.822910825 
         475          476          477          478          479          480 
 4.750213461  0.333814698  3.669693651  2.419805937  0.545465177  0.851112821 
         481          482          483          484          485          486 
 1.796899789  4.381171022  1.428787046  6.618562052  1.981808341  2.898379465 
         487          488          489          490          491          492 
 4.659597821  8.698427651  8.338040435  3.580130861  0.593177355  7.540711014 
         493          494          495          496          497          498 
 0.880808864  2.852968465  0.977674926  0.917384897  0.751613159  0.397899065 
         499          500          501          502          503          504 
 3.754276493  0.399449397  1.000565480  0.979945876  2.397566344  5.263825779 
         505          506          507          508          509          510 
 1.500887519  0.556404047  2.250189570  5.081832079  7.612294106  0.945893649 
         511          512          513          514          515          516 
 0.417440760  0.762586273  0.881976517  6.924831145  0.416076855  0.313096903 
         517          518          519          520          521          522 
 1.698788875  1.571980278  2.382211072  2.335398462  0.808777247  0.332719900 
         523          524          525          526          527          528 
 3.248697459  0.515274048  0.191094324  2.265436030  0.599119942  0.508611309 
         529          530          531          532          533          534 
 0.428075670  2.212952544  0.463761916  0.352454562  0.925609388  1.657284645 
         535          536          537          538          539          540 
 0.330225455  0.315324891  0.849592141  0.246265465  5.683251152  2.685665941 
         541          542          543          544          545          546 
 0.159464362  0.144749382  0.434161426  0.914926933  0.279924539  0.752641095 
         547          548          549          550          551          552 
 0.378937178  0.198264525  0.156441470  0.127113093  0.585831722  1.728382034 
         553          554          555          556          557          558 
 0.834243895  2.261492194  0.323016288  0.357745888  0.761505549  2.407468989 
         559          560          561          562          563          564 
 0.304576233  2.629076863  0.404900739  0.714781898  2.426439046  0.472006599 
         565          566          567          568          569          570 
 0.322854210  2.797427854  0.345026562  2.616930006  0.569869232  0.807208619 
         571          572          573          574          575          576 
 0.059898757  0.665997196  0.370959144  2.346359213  0.041038771  0.287973666 
         577          578          579          580          581          582 
 0.215228943  2.983084688  1.284400290  0.307688596  0.341851275  2.039107634 
         583          584          585          586          587          588 
 0.221027943  2.559661802  0.492738005  0.046215031  0.295389415  0.186752263 
         589          590          591          592          593          594 
 0.256309195  1.687585264  0.136925435  0.686472811  0.259220001  0.132700535 
         595          596          597          598          599          600 
 5.841913703  0.067085190  0.088354585  1.778163693  0.555626899  0.419058809 
         601          602          603          604          605          606 
 3.215060842  0.483693143  0.324181280  0.652183894  0.053432310  3.949401989 
         607          608          609          610          611          612 
 1.021956996  2.476573074  0.236075873  0.298624046  0.058419988  1.706406668 
         613          614          615          616          617          618 
 0.635436307  1.648275917  0.508729223  0.210895517  2.602523526  1.103833210 
         619          620          621          622          623          624 
 0.115804942  0.061096171  0.813879207  0.781319893  0.337162220  0.306586730 
         625          626          627          628          629          630 
 2.701530460  1.384468297  0.177347018  2.198232515  0.164880093  3.692138483 
         631          632          633          634          635          636 
 2.976804746  0.321295302  0.089944834  2.713756016  3.013610964  1.857772525 
         637          638          639          640          641          642 
 0.055991091  0.594671063  0.128753664  0.186348906  0.330821579  0.948174432 
         643          644          645          646          647          648 
 4.719617726  0.628194958  0.197045193  0.471155412  2.126221578  0.799794605 
         649          650          651          652          653          654 
 0.204289894  0.568477114  0.305549248  0.818968803  0.623515127  0.313003952 
         655          656          657          658          659          660 
 0.110224217  0.116701379  0.220427869  2.311666592  3.444459402  0.145019012 
         661          662          663          664          665          666 
 0.819459606  0.340985657  0.707879417  1.499386786  1.461651128  2.669656031 
         667          668          669          670          671          672 
 0.743745305  0.662503498  0.754180648  0.124163955  3.946030639  0.184667763 
         673          674          675          676          677          678 
 1.022760120  0.107811587  0.473573122  0.457661714  0.718359762  1.461011024 
         679          680          681          682          683          684 
 0.428822116  0.119802966  1.272056766  4.545447550  0.354497834  1.208003675 
         685          686          687          688          689          690 
 1.774268009  2.611481873  1.287356219  5.031336240  0.606969435  1.823415622 
         691          692          693          694          695          696 
 0.902348277  0.283283191  0.285222773  1.322732484  2.425216758  1.222837562 
         697          698          699          700          701          702 
 1.776902123  0.266118103  0.403286137  1.154377312  0.417933461  4.602694831 
         703          704          705          706          707          708 
 1.560900360  0.604858847  2.712437444  1.125059407  0.803152561  4.250846958 
         709          710          711          712          713          714 
 0.403265624  0.988811430  0.664166239  0.299602608  1.197125758  3.158216469 
         715          716          717          718          719          720 
 1.432480665  1.192793856  1.144408776  2.248032395  2.487198546  7.652198740 
         721          722          723          724          725          726 
 0.464151385  0.509666753  0.742193981  0.767816432  5.951472429  3.390012020 
         727          728          729          730          731          732 
 2.974523933  0.878668152  1.152185633  0.926861541  2.324451191  7.514802195 
         733          734          735          736          737          738 
 3.730998260  0.436926059  2.760120440  2.735118105  0.528855148  0.757755270 
         739          740          741          742          743          744 
 4.079776558  1.258371809  0.739512768  0.818673237  7.596992570  0.615707482 
         745          746          747          748          749          750 
 3.636913185  3.207969217  1.655289947  0.907874704  1.897222775  0.721417060 
         751          752          753          754          755          756 
 1.012286204  0.419571869  1.809822100  2.071164090  2.867316921  2.242358148 
         757          758          759          760          761          762 
 4.208542015  1.379509839  1.374822948  1.141867454  2.546489509  0.451095825 
         763          764          765          766          767          768 
 7.373174531  1.601487123  1.374967616  4.289448526  1.905399056  0.924178296 
         769          770          771          772          773          774 
 4.378347551  4.213009850  1.648930090  0.513944115  5.647742964  1.750755006 
         775          776          777          778          779          780 
 0.814208607  0.995666895  0.781674614  5.778822070  0.318409932  0.446619636 
         781          782          783          784          785          786 
 0.262218839  0.485497062  0.616579919  0.321633399  0.334520498  0.772693862 
         787          788          789          790          791          792 
 1.357150774  1.048510426  1.569515044  1.558852448  0.751644028  0.663691270 
         793          794          795          796          797          798 
 1.334417824  0.853717185  1.753199531  2.845723093  0.553138213  0.868242129 
         799          800          801          802          803          804 
 0.347470902  1.545783162  0.710729403  0.510977867  0.541766908  0.320030181 
         805          806          807          808          809          810 
 0.590462753  0.463967893  0.194959072  0.411941083  1.489427310  0.454785248 
         811          812          813          814          815          816 
 0.604422472  0.568959208  0.291028335  0.348156234  1.707557375  0.819273401 
         817          818          819          820          821          822 
 3.567708237  1.143034589  0.219133454  5.676736377  3.133410059  0.782985063 
         823          824          825          826          827          828 
 0.113882810  0.203905548  0.193514245  0.433819599  0.119823213  0.125000371 
         829          830          831          832          833          834 
 0.205912404  1.303123109  0.958428142  1.248074867  1.294003886  0.240089684 
         835          836          837          838          839          840 
 0.231631486  0.369689148  0.273089738  0.316919253  1.613587569  0.173089866 
         841          842          843          844          845          846 
 3.199714376  0.323829170  1.764372476  0.648976320  0.269645890  0.605441025 
         847          848          849          850          851          852 
 0.967479284  0.489109783  0.313524094  0.550104354  0.116240056  1.305652584 
         853          854          855          856          857          858 
 2.580152402  0.423004855  0.436733545  0.507421956  1.078786491  0.234283508 
         859          860          861          862          863          864 
 0.416518554  0.285764469  0.265671729  0.159152464  0.071872844  2.670690927 
         865          866          867          868          869          870 
 0.285756002  0.751198417  0.669690527  0.087331376  0.175380383  1.006655840 
         871          872          873          874          875          876 
 0.284143316  2.081063896  0.730563194  3.256094186  0.173376466  0.612542759 
         877          878          879          880          881          882 
 0.047587148  0.091737932  0.230952161  1.369671393  0.118977341  0.296670397 
         883          884          885          886          887          888 
 0.279592242  0.181932492  1.679712242  0.250441503  0.185840596  2.103425373 
         889          890          891          892          893          894 
 5.424374949  0.882954598  0.873264494  0.152852955  1.671242786  1.278176790 
         895          896          897          898          899          900 
 0.128583373  2.751004687  0.072031128  0.171661694  0.207548710  1.432621113 
         901          902          903          904          905          906 
 1.569638161  0.065605183  0.107437410  0.149602328  0.777808209  0.392975768 
         907          908          909          910          911          912 
 0.290803820  4.491587887  0.217536326  0.605732263  1.288175693  0.422565958 
         913          914          915          916          917          918 
 0.307129184  0.151057853  0.448038694  1.070717264  0.570563572  0.548198065 
         919          920          921          922          923          924 
 0.043556204  0.097031652  0.165588485  0.052220233  0.184542762  0.576617124 
         925          926          927          928          929          930 
 0.154931131  0.705675141  2.007663827  3.343750715  0.582197052  1.334101270 
         931          932          933          934          935          936 
 0.265039781  0.086751041  0.357755271  0.961283039  0.349622117  1.829586279 
         937          938          939          940          941          942 
 1.464298098  3.799402754  0.173548165  0.200221135  0.144116796  0.381448529 
         943          944          945          946          947          948 
 0.507333221  1.101265706  0.250244568  3.387462505  0.781077471  1.150787307 
         949          950          951          952          953          954 
 0.186607631  0.392404711  0.435921050  0.327417874  0.715047637  0.144133465 
         955          956          957          958          959          960 
 0.134530668  2.698860623  3.212113620  0.313128640  1.046849946  2.623098899 
         961          962          963          964          965          966 
 0.788862626  0.400406286  0.825739053  2.698784616  1.018036232  4.163933189 
         967          968          969          970          971          972 
 1.405223613  0.476823567  1.739980022  0.502323121  2.828521821  0.671408370 
         973          974          975          976          977          978 
 0.699352864  1.317512574  0.612277262  0.587220997  0.487713590  0.693689176 
         979          980          981          982          983          984 
 6.230916502  0.447215323  1.432911899  1.109021041  0.465843116  0.501564683 
         985          986          987          988          989          990 
 0.856522906  1.029591502  0.552110637  1.458590312  4.772931582  0.654772270 
         991          992          993          994          995          996 
 1.487897961  0.976107460  1.009957725  1.394919392  8.314150936  0.730267339 
         997          998          999         1000         1001         1002 
 1.095099587  0.561673641  0.612545434  3.486668831  0.861312277  8.782244105 
        1003         1004         1005         1006         1007         1008 
 3.031409456  0.779650856  1.410704775  1.816808202  1.332279101  0.672017680 
        1009         1010         1011         1012         1013         1014 
 0.794440745  1.190906361  2.237288492  0.542049421  2.257502773  2.044533800 
        1015         1016         1017         1018         1019         1020 
 1.483151678  1.189860905  5.816064125  2.181583802  0.914652603  1.311425659 
        1021         1022         1023         1024         1025         1026 
 1.831284943  5.579580713  4.904180763  1.389003422  1.377298775  3.351553282 
        1027         1028         1029         1030         1031         1032 
 0.979236388  6.043235219  0.756185783  2.119186052  3.554319286  0.688527366 
        1033         1034         1035         1036         1037         1038 
 2.239039165  0.289048422  1.199174882  6.329140086  1.524569026  2.638680225 
        1039         1040         1041         1042         1043         1044 
 0.533196938  2.268867073  1.526367491  6.467254622  1.297414269  0.625529333 
        1045         1046         1047         1048         1049         1050 
 3.174140516  0.512634777  7.586726259  0.826988095  0.774133469  1.333476360 
        1051         1052         1053         1054         1055         1056 
 0.223832981  1.543860499  1.932358723 15.754246414  0.313755026  0.794239576 
        1057         1058         1059         1060         1061         1062 
 1.261574842  1.004008419  0.762913307  2.346609607  0.220065986  5.114617484 
        1063         1064         1065         1066         1067         1068 
 0.232599463  1.070515200  1.764293683  0.988720510  0.909721406  0.279259033 
        1069         1070         1071         1072         1073         1074 
 2.380961287  4.592840870  1.013539311  0.377117710  0.248732016  1.532005411 
        1075         1076         1077         1078         1079         1080 
 1.411812491  0.462177716  0.570740877  1.593607073  0.301540461  1.096372292 
        1081         1082         1083         1084         1085         1086 
 3.358071526  0.817521452  0.631773775  4.761316612  0.592666462  0.160520771 
        1087         1088         1089         1090         1091         1092 
 0.195193872  1.613643158  1.586691377  3.908245377  4.333069476  0.703619654 
        1093         1094         1095         1096         1097         1098 
 0.230142010  1.392379920  0.394579157  0.133992403  0.187666509  4.772139183 
        1099         1100         1101         1102         1103         1104 
 0.216190260  0.646534414  0.930464684  0.461015120  0.431696943  3.811833363 
        1105         1106         1107         1108         1109         1110 
 0.207496434  0.175288744  0.438815330  0.852818707  0.490036132  0.899456212 
        1111         1112         1113         1114         1115         1116 
 0.618350897  0.087752763  3.393364761  0.815907965  0.195019987  2.663453707 
        1117         1118         1119         1120         1121         1122 
 0.467741689  0.279159628  0.235320113  0.928748734  0.256399050  0.188349092 
        1123         1124         1125         1126         1127         1128 
 0.222798788  1.462620750  0.416549727  1.246646493  0.827849802  0.163658883 
        1129         1130         1131         1132         1133         1134 
 0.432934576  0.507280571  0.174689418  1.174979815  1.170158968  0.242868165 
        1135         1136         1137         1138         1139         1140 
 0.327267921  0.884280371  1.264470705  0.497348596  0.648191158  0.194174328 
        1141         1142         1143         1144         1145         1146 
 0.348733941  0.241002062  0.291533547  0.247607139  2.072273565  1.278031259 
        1147         1148         1149         1150         1151         1152 
 3.794653862  0.150439232  1.192568662  0.075401104  0.683435553  0.896936673 
        1153         1154         1155         1156         1157         1158 
 0.249830309  0.104794563  1.605475199  0.340154340  0.392975859  0.948155116 
        1159         1160         1161         1162         1163         1164 
 0.689686091  0.186095426  0.133697122  0.047787195  0.084728471  0.408390163 
        1165         1166         1167         1168         1169         1170 
 0.943586488  1.134249538  0.166382464  0.074217395  0.200200800  1.224668354 
        1171         1172         1173         1174         1175         1176 
 0.242389500  0.320928307  0.583030206  0.154463331  0.081002514  0.383773613 
        1177         1178         1179         1180         1181         1182 
 0.267320595  0.323063853  1.687043962  0.197526100  0.183918867  1.003540461 
        1183         1184         1185         1186         1187         1188 
 0.742171413  4.042422073  0.222620242  1.313606642  0.662164435  0.169694483 
        1189         1190         1191         1192         1193         1194 
 0.119973872  0.227189350  0.102085560  1.278761456  0.191832959  0.203008022 
        1195         1196         1197         1198         1199         1200 
 0.162256152  0.177203767  0.802661960  0.160638952  2.139813777  0.249959076 
        1201         1202         1203         1204         1205         1206 
 0.123792700  0.138064387  0.625199731  0.128113651  0.706630633  2.762135384 
        1207         1208         1209         1210         1211         1212 
 0.088227450  0.182471141  0.133027138  2.296004141  0.939506525  0.129138702 
        1213         1214         1215         1216         1217         1218 
 0.685636646  0.766684074  0.181661544  0.349756451  0.109992993  0.235842660 
        1219         1220         1221         1222         1223         1224 
 0.388246621  0.116703214  3.168190160  4.350758286  0.137389323  0.308512679 
        1225         1226         1227         1228         1229         1230 
 0.366394588  0.439487967  0.199765189  0.394925584  1.015624487  0.275420688 
        1231         1232         1233         1234         1235         1236 
 1.033346180  0.232484823  0.226132882  1.153189166  3.431949741  0.256313590 
        1237         1238         1239         1240         1241         1242 
 0.169893156  4.023495717  0.432144485  0.281792434  0.318871842  2.517484068 
        1243         1244         1245         1246         1247         1248 
 0.926961415  0.424805179  0.725624053  2.445536063  0.815304661  3.066527521 
        1249         1250         1251         1252         1253         1254 
 5.515128424  2.359523282  1.184098336  1.047385961  0.959288726  0.194449616 
        1255         1256         1257         1258         1259         1260 
 0.451430879  6.044835263  0.472015548  6.131889235 11.938022552  5.619543253 
        1261         1262         1263         1264         1265         1266 
 1.646545195  6.228464455  0.500835961  0.565764877  0.489472006  1.682476629 
        1267         1268         1269         1270         1271         1272 
 1.664301497  1.026008909  0.582682972  4.158303889  0.597005324  0.714133102 
        1273         1274         1275         1276         1277         1278 
 2.294727994  1.512397735  4.894612820  1.266621514  2.112198074  1.577795372 
        1279         1280         1281         1282         1283         1284 
 0.554749831  1.546510494  1.172572870  0.283097923  0.440292457  1.495324965 
        1285         1286         1287         1288         1289         1290 
 1.210806640  1.152802615  1.831194064  1.505622404 14.331473307  3.236364037 
        1291         1292         1293         1294         1295         1296 
 1.504015703  0.918603703  3.387154160  1.569899183  0.977793548  2.398637038 
        1297         1298         1299         1300         1301         1302 
 0.754769309  1.511483775  2.099961005  0.605162119  0.826379241  1.523521608 
        1303         1304         1305         1306         1307         1308 
 5.939658914  1.139733970  1.287540227  5.196741061  2.267269111  0.575780334 
        1309         1310         1311         1312         1313         1314 
 2.325079749  1.620954493  2.118303537  0.851296256  2.009079514  0.640133568 
        1315         1316         1317         1318         1319         1320 
 1.066779870  7.272057662  1.100840136  0.421673487  1.881294700  2.103936147 
        1321         1322         1323         1324         1325         1326 
 7.377278311  0.838708314  0.506791133 17.067229636  1.139693806  0.662331652 
        1327         1328         1329         1330         1331         1332 
 0.919979462  2.322778831  1.640543002  0.206907100  1.115740349  2.235118904 
        1333         1334         1335         1336         1337         1338 
 1.663213253  0.905114104  0.831023683  2.172395558  1.111954535  4.489645849 
        1339         1340         1341         1342         1343         1344 
 0.995081675  2.436281508  0.657676824  1.787664921  3.351395681  1.612586595 
        1345         1346         1347         1348         1349         1350 
 1.781981748  0.468076663  7.128135968  4.624616317  6.576320299  2.731723444 
        1351         1352         1353         1354         1355         1356 
 2.538065926  1.380584816  1.674770581  0.376534104  2.033100480  1.729611536 
        1357         1358         1359         1360         1361         1362 
 0.851627858  8.120079805  0.275248780  0.251880942  1.307773199  0.730735670 
        1363         1364         1365         1366         1367         1368 
 7.738330354  1.133302791  0.683460973  0.866208542  1.577769909  1.491947512 
        1369         1370         1371         1372         1373         1374 
 5.097276250  0.720323263  0.252366333  3.005786872  0.399123524  4.347426480 
        1375         1376         1377         1378         1379         1380 
 4.430553047  1.438405365  0.687959148  0.376427596  1.502687511  2.908464852 
        1381         1382         1383         1384         1385         1386 
11.265189637  1.365334065  0.276984774  0.179086889  1.156748410  0.383819435 
        1387         1388         1389         1390         1391         1392 
 0.763139717  5.243811630  0.663041074  1.422956999  0.232870389  1.047557638 
        1393         1394         1395         1396         1397         1398 
 0.477040840  1.512772116  0.379697787  1.799870790  0.522280834  0.218461498 
        1399         1400         1401         1402         1403         1404 
 1.707732501  8.311554310  0.375063029  0.268035727  0.486389000  0.706756009 
        1405         1406         1407         1408         1409         1410 
 0.955475964  1.458513616  4.641145469  0.234941122  0.259327713  0.467351823 
        1411         1412         1413         1414         1415         1416 
 0.411097786  0.233729696  0.400667263  1.295192088  0.254846322  0.873692073 
        1417         1418         1419         1420         1421         1422 
 2.416639995  0.097816476  2.703787917  0.242167712  0.321221721  0.289828779 
        1423         1424         1425         1426         1427         1428 
 0.312007734  1.488977993  1.371394709  4.421120503  0.465171322  0.624281618 
        1429         1430         1431         1432         1433         1434 
 0.892621099  0.205692966  1.524350944  0.160595392  0.101412623  0.440878323 
        1435         1436         1437         1438         1439         1440 
 0.538717198  0.275866285  0.370263567  0.176380010  0.269113229  0.349016571 
        1441         1442         1443         1444         1445         1446 
 0.292362327  0.144956598  3.343414655  0.274252385  0.167749102  0.220536234 
        1447         1448         1449         1450         1451         1452 
 0.065701281  1.394635173  0.929074278  0.072143483  0.097813791  0.031431060 
        1453         1454         1455         1456         1457         1458 
 0.465377608  0.354467992  0.207211490  3.893555896  0.311050228  1.182554562 
        1459         1460         1461         1462         1463         1464 
 0.160220257  1.235713728  0.306843617  0.083388981  0.589378524  0.170969924 
        1465         1466         1467         1468         1469         1470 
 0.389061818  0.234484817  1.198254990  0.345126411  0.433388664  1.136063117 
        1471         1472         1473         1474         1475         1476 
 0.277253569  0.165234349  0.413952391  0.060965920  0.463632685  0.279323440 
        1477         1478         1479         1480         1481         1482 
 0.178740904  0.608231588  1.165940405  0.092988105  0.273045170  0.476987319 
        1483         1484         1485         1486         1487         1488 
 1.898260236  0.092985623  0.063696527  0.092586481  0.249602041  0.663119663 
        1489         1490         1491         1492         1493         1494 
 0.854601781  0.429373381  1.950506883  0.124555182  0.081314926  0.806168464 
        1495         1496         1497         1498         1499         1500 
 0.522084723  0.404541549  0.338077072  0.380606239  0.100254430  0.103678365 
        1501         1502         1503         1504         1505         1506 
 0.196169727  0.341594851  0.138055426  0.139520281  0.128788003  0.228731256 
        1507         1508         1509         1510         1511         1512 
 0.622548549  1.280566588  0.133375884  0.506488276  0.392629936  0.284890502 
        1513         1514         1515         1516         1517         1518 
 0.150858068  0.943160412  0.137605688  0.961521923  1.266867075  2.494393134 
        1519         1520         1521         1522         1523         1524 
 0.626367457  1.240457825  0.169012798  0.270656177  0.805144435  1.740524474 
        1525         1526         1527         1528         1529         1530 
 0.330651798  0.479788498  1.734986338  0.180931375  0.216806189  0.191038046 
        1531         1532         1533         1534         1535         1536 
 0.390723890  0.191615937  0.317080607  2.277178277  0.347280015  0.453375887 
        1537         1538         1539         1540         1541         1542 
 0.166467575  0.377844743  0.065396434  1.153987545  0.896402842  1.690653384 
        1543         1544         1545         1546         1547         1548 
 0.172803872  1.243620709  0.375006197  0.203228390  0.597077011  0.349388733 
        1549         1550         1551         1552         1553         1554 
 0.824509915  0.376931096  0.184249925  4.159987361  2.016117677  3.318246366 
        1555         1556         1557         1558         1559         1560 
 0.227623275  0.290017600  3.429547115  0.780847631  0.559635995  0.334879102 
        1561         1562         1563         1564         1565         1566 
 0.399124720  1.047429756  4.430326209  0.359189496  0.218101436  0.225104861 
        1567         1568         1569         1570         1571         1572 
 0.836938751  0.929730205  0.495647025  2.875490262  0.852946528  1.592857536 
        1573         1574         1575         1576         1577         1578 
 1.542083210  3.190227831  1.306558945  1.473870057  1.873548734  1.395309999 
        1579         1580         1581         1582         1583         1584 
 0.651269745  2.199385103  1.553040377  1.592713047  1.787644414  0.430347880 
        1585         1586         1587         1588         1589         1590 
 2.137576647  0.713131688  0.670234059  1.307594405  1.948486692  3.545707767 
        1591         1592         1593         1594         1595         1596 
 4.245699839  2.324657432  0.889793416  1.113009799  4.230292983  3.247328614 
        1597         1598         1599         1600         1601         1602 
 5.493215269  0.498181114  2.062123741  5.687264592  6.001447547  5.937284356 
        1603         1604         1605         1606         1607         1608 
 0.367299048  5.803944486  5.390946569  0.292413931  1.807992983  2.181125838 
        1609         1610         1611         1612         1613         1614 
 2.122746429  0.705273941  2.007617288  2.090396371  2.179073542  1.142648011 
        1615         1616         1617         1618         1619         1620 
 1.052817105  7.906706842  0.415693571  1.093394817  4.345094734  1.413812595 
        1621         1622         1623         1624         1625         1626 
 1.768407610  3.504028719  4.737532619  0.501881914  4.131064775  1.983806320 
        1627         1628         1629         1630         1631         1632 
 1.223845396  1.716690299  0.667166470  5.568970966  0.675369911  0.182988105 
        1633         1634         1635         1636         1637         1638 
 1.216619466  0.417116925  2.111909368  0.312646952  1.186899636  0.924124046 
        1639         1640         1641         1642         1643         1644 
 0.450775428  1.883836740  0.262484390  0.642639966  1.539980683  0.158651847 
        1645         1646         1647         1648         1649         1650 
 0.527284254  0.530828544  0.516677312  0.922215138  0.947761878  0.218331432 
        1651         1652         1653         1654         1655         1656 
 3.380128566  0.925738719  0.355272067  2.867916401  1.368055395  0.394143029 
        1657         1658         1659         1660         1661         1662 
 0.200096258  0.373242787  0.050628163  0.139098856  0.067334994  0.313399779 
        1663         1664         1665         1666         1667         1668 
 0.400311064  1.614149600  0.129502522  1.055914053  0.714030748  1.878940211 
        1669         1670         1671         1672         1673         1674 
 0.398534123  0.251017945  0.571831849  0.508485075  1.171186065  2.199443527 
        1675         1676         1677         1678         1679         1680 
 0.228788944  0.125205235  0.533695402 -0.538659734  0.386941681  0.297161157 
        1681         1682         1683         1684         1685         1686 
 0.386370341  0.068105087  0.398942466  0.026211365  0.771262092  0.761758133 
        1687         1688         1689         1690         1691         1692 
 0.314338172  0.484648317  0.886945921  0.092256148  0.483332996  0.287617276 
        1693         1694         1695         1696         1697         1698 
 0.268855918  1.080283295  3.093544040  0.090864802  0.121080011  1.199892706 
        1699         1700         1701         1702         1703         1704 
 0.752196641  0.840429113  0.834981104  0.100838728  0.090195927  0.477742267 
        1705         1706         1707         1708         1709         1710 
 0.166577811  0.241297014 -0.340930702  0.182389627 -0.468093976  0.340814447 
        1711         1712         1713         1714         1715         1716 
 0.087036367  0.385905616  0.167032025  1.001012086  0.263162394  5.949135755 
        1717         1718         1719         1720         1721         1722 
 1.455883064  0.187841642  0.484268841  0.084720452  0.186127869  0.747963309 
        1723         1724         1725         1726         1727         1728 
 0.074357088  0.360888696  0.122486147  0.007637823  0.133595843  0.531517457 
        1729         1730         1731         1732         1733         1734 
 0.681009384  6.875788092  2.060721607  0.149519093  1.431519074  0.207191692 
        1735         1736         1737         1738         1739         1740 
-0.290862152  0.383008875  0.139544717  0.218581870  0.451250563  0.277409419 
        1741         1742         1743         1744         1745         1746 
 0.103005514  0.104889920  0.450195593  2.743240945  0.199002451  2.057297910 
        1747         1748         1749         1750         1751         1752 
 1.793615271  0.079395626  0.654798269  0.866653301  3.135160603  0.485839133 
        1753         1754         1755         1756         1757         1758 
 0.689533625  0.184827807  1.968390020  1.412099876  0.361374279  0.260403457 
        1759         1760         1761         1762         1763         1764 
 1.065135065  0.058599934  7.490325907  0.918539923  0.102147284  1.040698894 
        1765         1766         1767         1768         1769         1770 
 2.346275328  0.903855700  5.077222682  0.352690368  3.655135185  2.098104915 
        1771         1772         1773         1774         1775         1776 
 2.427854206  0.393704425  0.330778725  0.438554213  0.857527050  0.401400512 
        1777         1778         1779         1780         1781         1782 
 0.253215849  6.278722503  1.364958528  0.549883341  0.599816802  3.677415487 
        1783         1784         1785         1786         1787         1788 
 3.665837844  4.603685518  0.604129123  1.528503123  0.798374958  2.261134317 
        1789         1790         1791         1792         1793         1794 
 1.469359006  0.186194181  1.081106551  0.388850928  2.426078891  0.739642579 
        1795         1796         1797         1798         1799         1800 
 1.932137161  0.777214764  1.235699020  5.021303265  2.082367044  4.986257259 
        1801         1802         1803         1804         1805         1806 
 0.893825211  1.727916650  3.318694957  0.953633867  1.148079350  4.434596977 
        1807         1808         1809         1810         1811         1812 
 0.577673324  5.844755789  8.007369756  0.700570310  2.978351307  1.867177738 
        1813         1814         1815         1816         1817         1818 
 1.588656177  1.321536256  6.510804233  1.401017276  1.258352938  2.644313487 
        1819         1820         1821         1822         1823         1824 
 1.610667593  0.638359831  3.071783013  1.466506465  0.198742399  1.220287378 
        1825         1826         1827         1828         1829         1830 
 3.349078135  1.724866176  1.393344950  0.863721734  1.009947584  4.546209952 
        1831         1832         1833         1834         1835         1836 
 0.626058347  4.135555269  5.885135112  1.244786888  1.198527106  0.848928610 
        1837         1838         1839         1840         1841         1842 
 0.510977263  1.291954900  0.928567898  3.624009337  1.958661383  2.977041545 
        1843         1844         1845         1846         1847         1848 
 0.669452869  0.740918706  0.419229788  3.688144941  0.725907930  0.603720371 
        1849         1850         1851         1852         1853         1854 
 7.037566955  0.777255794  0.628735497  6.341245340  3.152338403  0.630139705 
        1855         1856         1857         1858         1859         1860 
 0.546856903  0.588737572  0.198295824  4.965625566  0.186883597  1.340908642 
        1861         1862         1863         1864         1865         1866 
 1.101026594  1.860742750  1.386376309  0.674212418  0.088279356  0.659654475 
        1867         1868         1869         1870         1871         1872 
 0.618029944  0.326013740  0.182006711  0.541765795  0.149539757  0.354666284 
        1873         1874         1875         1876         1877         1878 
 1.027448839  0.166783745  0.528637024  2.039648539  5.622906874  0.433337366 
        1879         1880         1881         1882         1883         1884 
 1.307855634  0.458797609  0.350378347  1.127463212  0.260706491  0.545876849 
        1885         1886         1887         1888         1889         1890 
 0.909055830  0.965707308  0.425256288  0.438555382  0.278587981  0.116073520 
        1891         1892         1893         1894         1895         1896 
 1.522961021  0.549055202  0.379720335  0.339652057  1.047915493  0.387034066 

Compare the observed (FCH4_F_gC) versus predicted (PRED.TPVPD):

ggplot() + geom_point( data = train, aes( x=FCH4_F_gC, y= PRED.TPVPD )) +
  geom_smooth(method='lm')

summary(lm(data=train,  PRED.TPVPD~FCH4_F_gC))

Call:
lm(formula = PRED.TPVPD ~ FCH4_F_gC, data = train)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.8501 -0.3881 -0.2159  0.2347  4.1151 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.519090   0.015808   32.84   <2e-16 ***
FCH4_F_gC   0.639029   0.005478  116.65   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.6003 on 1894 degrees of freedom
Multiple R-squared:  0.8778,    Adjusted R-squared:  0.8777 
F-statistic: 1.361e+04 on 1 and 1894 DF,  p-value: < 2.2e-16

See how well the model performs on data that was not used to train the model:

test$PRED.TPVPD <- predict(FCH4_F_gC.rf, test)
  
ggplot() + geom_point( data = test, aes( x=FCH4_F_gC, y= PRED.TPVPD )) +
  geom_smooth(method='lm')

summary(lm(data=test,  PRED.TPVPD~FCH4_F_gC))

Call:
lm(formula = PRED.TPVPD ~ FCH4_F_gC, data = test)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.6482 -0.8116 -0.4033  0.4567  5.9730 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  1.06617    0.06553   16.27   <2e-16 ***
FCH4_F_gC    0.32516    0.02412   13.48   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.235 on 464 degrees of freedom
Multiple R-squared:  0.2814,    Adjusted R-squared:  0.2799 
F-statistic: 181.7 on 1 and 464 DF,  p-value: < 2.2e-16

Final Model Development:

The current model includes only climate variables from the tower. Use either a forward or backward selection method to develop your final model using your own data sets.

The forward selection approach starts with no variables and adds each new variable incrementally, testing for statistical significance, while the backward elimination method begins with a full model and then removes the least statistically significant variables one at a time.

Save your final model and datasets in a .Rdata object for next class where we will perform sensitivity analyses on the models.

save( FCH4_F_gC.rf , file="data/products/FinalModel.RDATA")