hen I run the script below in the R console, it runs, but when I use RStudio I get this:
Error in sink(type = "output") : invalid connection
I have been working my way through Richard McElreath, Statistical Rethinking: A Bayesian Course with Examples in R and Stan, 2nd ed. McElreath uses an R package, Rethinking, which contains data for analysis and software for analysis. Based on the author’s strong suggestion I have used Rstudio and become comfortable with it. In Chapter 9, McElreath moves to use Markov Chains for analysis, relying on Stan. At this transition, I consistently get the error message: Error in sink(type = "output") : invalid connection. I struggled with installing Rstan and StanHeaders packages—learning along the way.
I finally tried running the example code in R alone. What generated the error in RStudio ran without difficulty in R.
I have done this using Microsoft 4.0.2 release and the most recent R 4.0.4. I have been using Rstudio Version 1.4.1103. I have tried several of the earlier RStudio releases and have had the same problem. Unfortunately, I have become used to having the advantages of RStudio in working with R.
Below is the code example. R Code 11.14, the first to use Ulam, generated the error: Error in sink(type = "output") : invalid connection
R code 9.11
library(rethinking)
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp / mean(dd$log_gdp)
dd$rugged_std <- dd$rugged / max(dd$rugged)
dd$cid <- ifelse( dd$cont_africa==1 , 1 , 2 )
# R code 9.12
m8.3 <- quap(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~exp( 1 )
) , data=dd )
precis( m8.3 , depth=2 )
# library(parallel)
# options(mc.cores = parallel::detectCores())
# Sys.setenv(LOCAL_CPPFLAGS = '-march=native')
# R code 9.13, Februry 13, 2021
dat_slim <- list(
log_gdp_std = dd$log_gdp_std,
rugged_std = dd$rugged_std,
cid = as.integer( dd$cid )
)
str(dat_slim)
# R code 9.14, February 13, 2021
m9.1 <- ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
) , data=dat_slim , chains = 5 )