0

I’m using the rethinking package in R to make a simple linear model. In the folowing code I use a prior normal distribution for the dependent variable and everything works good.

library(rethinking)
col <- alist(
  courework_n ~ dnorm(mean,0.2),
  mean <- a + b*result_n + c,
  a ~ dnorm(0,10),
  b ~ dnorm(0,10),
  c ~ dnorm(0,10)
)
colmap <- map( col , data.frame(data) )

But when I use a package for triangular distribution which is not supported directly by R, I get the following error

Error in map(col, data.frame(data)) : unused argument (log = TRUE)

This is the code for defining the model with the triangle distribution

install.packages("RTriangle") 
library(triangle)

col <- alist(
  courework_n ~ dtriangle(0,1,mode),
  moda <- a + b*result_n + c,
  a ~ dnorm(0,10),
  b ~ dnorm(0,10),
  c ~ dnorm(0,10)
)

col_map <- map( col , data.frame(data) )
Oscar Muñoz
  • 439
  • 1
  • 5
  • 18
  • Triangular priors can have the same priors have the same issue with unintended bias due to boundary position as uniform priors on an interval. If mass would've extended beyond the boundary, the boundary pushes it away from the boundary pushing the posterior estimate away from the boundary. So even if the unbounded estimate would've been near the boundary, the existence of the boundary pushes it away from the boundary, which is usually unintended for these kinds of priors (which are usually motivated by wanting to identify rough interval of solution and highest density point). – Bob Carpenter Nov 26 '17 at 01:50

1 Answers1

1

Because the dtriangle distribution doesn't incorporate a log parameter as is expected of probability distribution functions in R/by rethinking.

You can hack the dtriangle function as follows:

  • first dput(dtriangle,file="my_dtriangle.R") to save the function code to a file
  • edit the first line to read
 my_dtriangle <- function (x, a = 0, b = 1, c = (a + b)/2, log=FALSE)
  • change the last line of code (return(apply(params, 1, dTest))) to:
 res <- apply(params, 1, dTest)
 if (log) return(log(res)) else return(res)
  • source("my_dtriangle.R") to redefine the function in your workspace

It's conceivable that you will run into further technical problems using a function that has compact support (i.e., has zero likelihood outside of a restricted range of values)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks a lot, this is great trick that can be used for lots of other things, I appreciate too much your help. Anyway you were right, now I’m having a problem because the dtriangle function requires the x value for its computation (as all the others d-distributions in R) but I supposed that when using ej: dnorm and omitting the x value, rethinking uses another file of the stats family functions. – Oscar Muñoz Nov 24 '17 at 22:10
  • 1
    Would it be possible to just write a wrapper function using `...` and not have to cut-and-paste? When you have a log scale, you can often more robustly do your calculations on the log scale directly. – Bob Carpenter Nov 26 '17 at 01:48