# Converting a BUGS model to Turing, simple linear regression

**URL:** https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340
**Category:** Probabilistic Programming
**Created:** [August 30, 2021, 11:31am UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340 "2021-08-30T11:31:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![areding](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/areding/32/21083_2.png) [@areding](https://discourse.julialang.org/u/areding)
#### Post date: [August 30, 2021, 11:31am UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340/1 "2021-08-30T11:31:50Z")

</div>

Hello everyone,

I’m thinking about translating a course that focuses on WinBUGS to Turing so the students can use something a little more modern if they want. I’m new to Julia and Turing.

Here is the WinBUGS model:

```julia
for (i in 1:N) {
    Y[i] ~ dnorm(mu[i],tau)
    mu[i] <- alpha + beta * (x[i] - x.bar) 
}
x.bar <- mean(x[])
alpha ~ dnorm(0, 0.0001)
beta ~ dnorm(0, 0.0001)
tau ~ dgamma(0.001, 0.001)
sigma <- 1.0/sqrt(tau)
}
#-----------------------------
DATA
list(N=5, x=c(1,2,3,4,5), Y=c(1,3,3,3,5)) #-----------------------------
INITS
list(alpha = 0.1, beta = 0.6, tau = 1)

```

and here is my attempt using Turing:

```julia
using Turing, Distributions
using DataFrames
using Random
using Statistics
Random.seed!(0)

data = DataFrame(x = [1, 2, 3, 4, 5], y = [1, 3, 3, 3, 5])

@model function simplereg(x, y)	
	# intercept prior
	α ~ Normal(0, 10e-4)
	
	# coefficient prior
	β ~ Normal(0, 10e-4)
	
	# variance prior
	τ ~ Gamma(10e-3, 100)
	σ = 1/sqrt(τ)

	for i in 1:length(x)
		y[i] ~ Normal(α + β*(x[i]-mean(x)), σ)
	end
end

```

```julia
iterations = 10000
	
model = simplereg(data.x, data.y)
chain = sample(model, MH(), iterations)

```

This runs, but the results are wildly different: for Turing, mean alpha is .0001, beta is .00003, tau is .1. For BUGS, mean alpha is about 3, beta about .8, tau about 1.9.

Also, I didn’t see a simple way to provide initial values - how is that handled in Turing?

I saw a way to use burn-in depending on which sampler is selected, but MH doesn’t use it. Why is that?

---

<div class="post-metadata">

### Author: ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)
#### Post date: [August 30, 2021, 12:41pm UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340/2 "2021-08-30T12:41:51Z")

</div>

Distributions.jl (which Turing and most of the Julia ecosystem uses) uses a normal distribution parameterized by a variance, instead of a precision (1/variance). So your Turing code should use something like `Normal(0, 10e4)` instead.

You’ll also want to check how Gamma is parametrized – I don’t remember off the top of my head if Distributions.jl is different than WinBUGS in that regard. (Edit - looks like you did change how Gamma was parameterized from WinBUGS, so I guess you already checked.)

---

<div class="post-metadata">

### Author: ![areding](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/areding/32/21083_2.png) [@areding](https://discourse.julialang.org/u/areding)
#### Post date: [August 30, 2021, 1:53pm UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340/3 "2021-08-30T13:53:46Z")

</div>

Woops, I overlooked that. Time to double-check all the parameterizations. Thanks for catching that, what a silly mistake.

---

<div class="post-metadata">

### Author: ![areding](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/areding/32/21083_2.png) [@areding](https://discourse.julialang.org/u/areding)
#### Post date: [August 30, 2021, 2:52pm UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340/4 "2021-08-30T14:52:22Z")

</div>

```julia
@model function simplereg(x, y)	
	α ~ Normal(0, 100)
	β ~ Normal(0, 100)
	τ ~ Gamma(.001, 1000)
	σ = 1/sqrt(τ)

	for i in 1:length(x)
		y[i] ~ Normal(α + β*(x[i] - mean(x)), σ)
	end
end

```

Okay, I think this should be equivalent, but the results are still way off. Tau is coming out at .001 now. What am I missing here?

---

<div class="post-metadata">

### Author: ![opera\_malenky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/opera_malenky/32/8213_2.png) [@opera\_malenky](https://discourse.julialang.org/u/opera_malenky)
#### Post date: [August 30, 2021, 3:20pm UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340/5 "2021-08-30T15:20:49Z")

</div>

I don’t think there’s a problem with your model; I think it’s just an issue with the default MH settings being a very bad fit for these parameters (when I ran the code, the Rhat looked fine, but that was apparently very misleading). If you use Nuts, the estimates match what you report for WinBUGS.

---

<div class="post-metadata">

### Author: ![areding](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/areding/32/21083_2.png) [@areding](https://discourse.julialang.org/u/areding)
#### Post date: [August 31, 2021, 2:56am UTC](https://discourse.julialang.org/t/converting-a-bugs-model-to-turing-simple-linear-regression/67340/6 "2021-08-31T02:56:12Z")

</div>

NUTS did the trick! Thank you very much.
