Chose the best model and use it for predictions

Hello

I would like to to run several models (ols to ml) and automatically select the model with best rmse or R2 and use this best model fit to de prediction on r shiny. Does any one has a piece of code to learn from (even with fake models)?

Thank you,

Check out Comparing Models with Resampling in the tidymodels books.

Thank you for your message please let me test and come back.

Oups it did not work out perhaps because of different model class:
model_list <- list(fitols, fitlasso)
resa <- resamples(model_list)
summary(resa)

on the documentation that was linked to I dont see that resample( appears anywhere ; The closest I see is
theres an implicit call to fit_resamples as part of the workflow_map() call

You are right but I felt it was not easy and fully related to my questions on how to select the best mode
Fit automatically to run it for prediction

I found a solution like this one where i extract the name of a regression fit like "reg_ols" corresponding the the max R2 then use it inside a predict function: get((rownames(FOO[which.max(fun$model_r2),])))

=> it works when a call this function but strangely i have a pb: when i run : predict (reg_ols, newdata) or predict (get((rownames(FOO[which.max(fun$model_r2),]))), new data) THE RESULT IS DIFFERENT ;()

Any idea? thanks a lot

We can't comment on code that we have no context from which to determine why it should be expected to run. It doesn't seem tidy at all to be relying on rownames. You're probably just not retreiving what you thought you were.

You are right, sorry. Indeed, i did several checks and realized i had another issue that is simple to understand :slight_smile: i use geocoder() to extract latitude/longitude using osm BUT at each code execution the geocoder() function send me back coordinates that are of course the same BUT with slight differences after two figures after the comma. So my question if i may: how to extract the max precision after comme each time? Thanks a lot

To help us help you, could you please prepare a repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I'm sorry. I ll try to do it indeed. I hesistated because i have a big code (i"m learning r shiny) so i have a extract the relevant piece of code. What i was saying is the if you give a postal adress to geocoder, you receive the correct lat/long but the slight variations after the commas.

What library/package provides you the function geocoder() ?

Please look here i found someone with the same question but still nor response :slight_smile:

i think i'm allowed to give the link (if not sorry, just delete it):

Tidygeocoder

Thank you

Firstly, you havent linked to any R code; thats another language (java? )
I dont follow understanding what your issue is.
geocode() from tidygeocoder will give you as much precision as it knows to give for the address you pass.
You can format this to text to percieve it at differing levels of precision, what do you mean by variations ? are you saying that you run the same address twice through geocode() and get different results each time ?

what is the actual problem ?

library(tidygeocoder)

(source_address <- data.frame(address="1600 Pennsylvania Ave NW Washington, DC"))
result1 <- geocode(source_address,
                   address,
                   method = 'arcgis')
result2 <- geocode(source_address,
                   address,
                   method = 'arcgis')

identical(result1,
          result2)
#TRUE 

# as text to 20 places
format(result1$lat,digits=20)
format(result2$lat,digits=20)
# "38.897675107650997006"
1 Like

Thanks a lot for your response. I will test on R your format() function that i did not know.
For info, i only use 'osm' because 'arcgis' gives me no response for european adresses (I'm perhaps wrong).
Thank you again

Last question if possible:
regarding sampling data: please what is the difference or what is the best option between:
-createdatapartition()
-sample()
personnally i find the sample() function intuitive and easier to carry.
Many thanks,