Movatterモバイル変換


[0]ホーム

URL:


Predicted values

This vignette demonstrates the versatility and utility of thepredict.modeler() function when applied to a fitted model.This function is designed to handle models of classmodelerand provides several prediction types, outlined as follows:

Each type of prediction includes corresponding standard errors, whichare calculated using the delta method.

library(flexFitR)library(dplyr)library(kableExtra)library(ggpubr)library(purrr)data(dt_potato)head(dt_potato)|>kable()
TrialPlotRowRangegidDAPCanopyGLI
HARS20_chips111W17037-2400.0000.0000000
HARS20_chips111W17037-24290.0000.0027216
HARS20_chips111W17037-24360.670-0.0008966
HARS20_chips111W17037-244215.1140.0322547
HARS20_chips111W17037-245675.4240.2326896
HARS20_chips111W17037-247699.8110.3345619

0. Model fitting

To illustrate the functionality ofpredict(), we use apotato dataset to fit logistic models of the form:

\[f(t) = \frac{L}{1 + e^{-k(t -t_0)}}\]

fun_logistic<-function(t, L, k, t0) L/ (1+exp(-k* (t- t0)))

For simplicity, we’ll focus on just two plots from the dataset (plot40 and plot 166) out of the total 196 plots available. After fitting themodel, we’ll take a closer look at the parameter estimates, visualizethe fitted curves, and start making predictions.

plots<-c(40,166)
mod_1<- dt_potato|>modeler(x = DAP,y = Canopy,grp = Plot,fn ="fun_logistic",parameters =c(L =100,k =4,t0 =40),subset = plots  )
print(mod_1)#>#> Call:#> Canopy ~ fun_logistic(DAP, L, k, t0)#>#> Residuals (`Standardized`):#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.#> -1.5910 -0.6313  0.0326 -0.1693  0.4665  1.2007#>#> Optimization Results `head()`:#>  uid    L     k   t0  sse#>   40 99.8 0.199 47.7 37.4#>  166 99.0 0.262 42.8 23.2#>#> Metrics:#>  Groups      Timing Convergence Iterations#>       2 0.4459 secs        100%   712 (id)
plot(mod_1,id = plots)

plot fit

1. Point prediction

To make point predictions, we use thepredict() functionand specify the\(x\) value(s) forwhich we want to compute\(\hat{f}(x)\). By default, the predictiontype is set to"point", so it is unnecessary to explicitlyincludetype = "point".

points<-predict(mod_1,x =55,type ="point",se_interval ="confidence")points|>kable()
uidfn_namex_newpredicted.valuestd.error
40fun_logistic5580.852822.912364
166fun_logistic5595.062551.625402

A great way to visualize this is by plotting the fitted curve andoverlaying the predicted points.

mod_1|>plot(id = plots,type =3)+color_palette(palette ="jco")+geom_point(data = points,aes(x = x_new,y = predicted.value),shape =8)

plot fit 2

You’ll also notice that predictions come with standard errors, whichcan be adjusted using these_interval argument to choosebetween"confidence" or"prediction"intervals, depending on the type of intervals you want to generate(sometimes referred to as narrow vs. wide intervals).

points<-predict(mod_1,x =55,type ="point",se_interval ="prediction")points|>kable()
uidfn_namex_newpredicted.valuestd.error
40fun_logistic5580.852823.994805
166fun_logistic5595.062552.697961

2. Integral of the function (area under the curve)

The area under the fitted curve is another common calculation,especially when trying to summarize the overall behavior of a functionover a specific range. To compute the AUC, settype = "auc"and provide the interval of interest in thex argument. Youcan also specify the number of subintervals for the trapezoidal ruleapproximation usingn_points (e.g.,n_points = 500 provides a high-resolution approximationhere).

\[ \text{Area} = \int_{0}^{T} \frac{L}{1 +e^{-k(t - t_0)}} \, dt \]

predict(mod_1,x =c(0,108),type ="auc",n_points =500)|>kable()
uidfn_namex_minx_maxpredicted.valuestd.error
40fun_logistic01086018.83389.57456
166fun_logistic01086450.84069.71235

3. Function of the parameters

In many cases, interest lies not in the parameters themselves but infunctions of these parameters. By using theformulaargument, we can compute user-defined functions of the estimatedparameters along with their standard errors. No additional arguments arerequired for this functionality.

predict(mod_1,formula =~ k/ L*100)|>kable()
uidfn_nameformulapredicted.valuestd.error
40fun_logistick/L * 1000.19907570.0173468
166fun_logistick/L * 1000.26446160.0312821
predict(mod_1,formula =~ (k* L)/4)|>kable()
uidfn_nameformulapredicted.valuestd.error
40fun_logistic(k * L)/44.9593010.3761384
166fun_logistic(k * L)/46.4803100.7140117

4. Derivatives

For those interested in the derivatives of the fitted function,predict.modeler() provides tools to compute both the first(\(f'(x)\)) and second order (\(f''(x)\)) derivatives at specifiedpoints or over intervals. While derivatives for logistic functions arestraightforward to compute analytically, this is not true for many otherfunctions. To address this,predict() employs a numericalapproximation using the “Richardson” method.

For the logistic function, the first derivative has the followingform:

\[ f'(t) = \frac{k L e^{-k(t -t_0)}}{\left(1 + e^{-k(t - t_0)}\right)^2} \]

And the second derivative the following:

\[f''(t) = \frac{k^2 L e^{-k(t -t_0)} \left(1 - e^{-k(t - t_0)}\right)}{\left(1 + e^{-k(t -t_0)}\right)^3}\] Here the first derivative tells us the growthrate, while the second derivative reveals how the growth rate isaccelerating or decelerating.

4.1. First derivative

To compute the first derivative, settype = "fd" in thepredict() function and provide points or intervals in thex argument. In case we just want to visualize the firstderivative we can use theplot() function.

plot(mod_1,id = plots,type =5,color ="blue",add_ci =FALSE)

plot 1 deriv

The\(x\)-coordinate where themaximum occurs can be found programmatically, and the correspondingvalue of\(\hat{f}(x)\) can be computedusing point predictions as follows:

interval<-seq(0,100,by =0.1)points_fd<- mod_1|>predict(x = interval,type ="fd")|>group_by(uid)|>summarise(max_fd =max(predicted.value),argmax_fd = x_new[which.max(predicted.value)]  )points_fd|>kable()
uidmax_fdargmax_fd
404.95930047.7
1666.48011742.8
mod_1|>plot(id = plots,type =3)+color_palette(palette ="jco")+geom_vline(data = points_fd,aes(xintercept = argmax_fd),linetype =2)+geom_label(data = points_fd,aes(x = argmax_fd,y =0,label = argmax_fd))+facet_wrap(~uid)+theme(legend.position ="none")

plot deriv

points_fd$y_hat<-sapply(X = plots,FUN = \(i) {    mod_1|>predict(x = points_fd[points_fd$uid== i,"argmax_fd"],id = i)|>pull(predicted.value)  })points_fd|>kable()
uidmax_fdargmax_fdy_hat
404.95930047.749.88846
1666.48011742.849.23131
mod_1|>plot(id = plots,type =3)+color_palette(palette ="jco")+geom_point(data = points_fd,aes(x = argmax_fd,y = y_hat),shape =8)

plot points

4.2. Second derivative

Similarly, the second derivative can be calculated by settingtype = "sd". This derivative shows how the growth rateitself is changing, helping to determine when growth starts to slow downor speed up.

plot(mod_1,id = plots,type =6,color ="blue",add_ci =FALSE)

plot 2 deriv

We can also identify where the second derivative reaches its maximumand minimum values, and plot these changes for a deeper understanding ofthe growth dynamics.

points_sd<- mod_1|>predict(x = interval,type ="sd")|>group_by(uid)|>summarise(max_sd =max(predicted.value),argmax_sd = x_new[which.max(predicted.value)],min_sd =min(predicted.value),argmin_sd = x_new[which.min(predicted.value)]  )points_sd|>kable()
uidmax_sdargmax_sdmin_sdargmin_sd
400.379326941.1-0.379323254.3
1660.653056937.8-0.653041947.9
mod_1|>plot(id = plots,type =3)+color_palette(palette ="jco")+geom_vline(data = points_sd,aes(xintercept = argmax_sd),linetype =2)+geom_vline(data = points_sd,aes(xintercept = argmin_sd),linetype =2)+facet_wrap(~uid)+theme(legend.position ="none")

plot deriv 2

5. Conclusions

Thepredict.modeler() function, as part of the modelingtoolkit, offers a range of useful predictions that can be tailored tovarious needs—whether it’s making point estimates, exploring the areaunder a curve, or analyzing derivatives. While the examples presentedhere showcase the flexibility and power of the function, they are justthe beginning. Every dataset and research question brings its own uniquechallenges, and we hope this vignette demonstrates howpredict.modeler() can help address those.


[8]ページ先頭

©2009-2025 Movatter.jp