How is the loo function recognized in R with Stan?

I do Bayesian statistics programming using loo package in R with Stan.
There wasn’t loo function on the console, though I run the code as follows.
R version is 3.4.1, and the loo package is 2.1.0.

library(rstanarm) 
library(loo) 
library(bayesplot)
library(ggplot2) 
options(mc.cores = paralles::detectCores())
scr <- "modelbeforeafter.stan" 
source("databeforeafter.R") 
data <- list(N=N,xA=xA,xB=xB)
par <-c("muA","muB", "SigmaA","SigmaB","log_likA","log_likB","rhoA", "rhoB","deltaA","deltaB","es","deltaA_over","deltaB_over","es_over","delta_r","delta_r_over")
war <- 1000 
ite <- 11000 
see <- 1234 
dig <- 3 
cha <- 1 
stanfit <- stan(file=scr, model_name=scr, data=data, pars=par, verbose=F,seed=see, chains=cha, warmup=war,iter=ite)
loo1 <- loo(stanfit,save_psis= TRUE)
print(loo1,inc_warmup = T, pars=par)
plot(loo1)

If you have Stan code, you should call stan() not stan_glm(). The above code should give error when calling stan_glm()

Hope this helps

Thank you very much for answering my question, but the loo function isn’t recognized in R with Stan, though I change stan_glm() into stan() , which you say.

Do you get some error message?

I have already installed loo package, but
on the console, there isn’t loo1.

Did you get any error message after either of these lines? If you did get an error message, please copy the whole error message here.

print() for loo objects doesn’t have options inc_warmup = T, pars=par. Did you get an error message from this line? If you did get an error message, copy the whole error message here.

The console message shows as follows.
loo1 <- loo(stanfit,save_psis= TRUE)
Error in loo(stanfit, save_psis = TRUE) : could not find function “loo”

And you didn’t get any error message when trying to load the package?

What is your question exactly? When you call loo() on a stanfit object (one generated by stan or sampling from the rstan package), then function loo.stanfit() is actually called. If you want to see it you can type rstan:::loo.stanfit. There are other implementation of that function that work for arrays or matrices which are provided by the loo package.

The console message shows as follows when trying to load the loo package.

install.packages(“loo”)
trying URL ‘https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/loo_2.1.0.tgz
Content type ‘application/x-gzip’ length 1350101 bytes (1.3 MB)
==================================================
downloaded 1.3 MB

tar: Failed to set default locale

The downloaded binary packages are in
/var/folders/96/k4pnmj4n1ql6mpyc2c650v2c0000gn/T//RtmpYbE7ua/downloaded_packages

Maybe this helps https://stackoverflow.com/questions/3907719/how-to-fix-tar-failed-to-set-default-locale-error ?

Thank you so much for telling me the site. I will try to do it over this weekends.

Thank you very much for answering my question. I will try to do what you say over this weekend.

When I try to do what the site says ( https://stackoverflow.com/questions/3907719/how-to-fix-tar-failed-to-set-default-locale-error), the error to load the loo package is solved. However, there is another problem about stan function I find today. I will post it to this stan site.

When I type rstan:::loo.stanfit, the loo() error is solved. However, there is another problem about stan function I find today. I will post it to this stan site.