如何在rmarkdown中使用stan

时间:2018-02-03 01:14:44

标签: r r-markdown stan rstan

我想在rnotebook中使用/opt/nginx/sites-enabled/website.conf获得模型的估计系数

我有以下rstan块:

stan

我还有以下数据

```{stan output.var="rats"}
data {
  int<lower=0> N;
  int<lower=0> T;
  real x[T];
  real y[N,T];
  real xbar;
}
parameters {
  real alpha[N];
  real beta[N];

  real mu_alpha;
  real mu_beta;          // beta.c in original bugs model

  real<lower=0> sigmasq_y;
  real<lower=0> sigmasq_alpha;
  real<lower=0> sigmasq_beta;
}
transformed parameters {
  real<lower=0> sigma_y;       // sigma in original bugs model
  real<lower=0> sigma_alpha;
  real<lower=0> sigma_beta;

  sigma_y = sqrt(sigmasq_y);
  sigma_alpha = sqrt(sigmasq_alpha);
  sigma_beta =  sqrt(sigmasq_beta);
}
model {
  mu_alpha ~ normal(0, 100);
  mu_beta ~ normal(0, 100);
  sigmasq_y ~ inv_gamma(0.001, 0.001);
  sigmasq_alpha ~ inv_gamma(0.001, 0.001);
  sigmasq_beta ~ inv_gamma(0.001, 0.001);
  alpha ~ normal(mu_alpha, sigma_alpha); // vectorized
  beta ~ normal(mu_beta, sigma_beta);  // vectorized
  for (n in 1:N)
    for (t in 1:T) 
      y[n,t] ~ normal(alpha[n] + beta[n] * (x[t] - xbar), sigma_y);

}
generated quantities {
  real alpha0;
  alpha0 = mu_alpha - xbar * mu_beta;
}
```

github上的documentation显示```{r} df <- read_delim("https://raw.githubusercontent.com/wiki/stan-dev/rstan/rats.txt",delim = " ") y <- as.matrix(df) x <- c(8,15,22,29,36) xbar <- mean(x) N <- nrow(y) T <- ncol(y) ``` ,但由于我使用了一个块,因此我没有要引用的文件。

我尝试了rats_fit <- stan(file = 'https://raw.githubusercontent.com/stan-dev/example-models/master/bugs_examples/vol1/rats/rats.stan')stan(rats)summary(rats),但这些似乎都不起作用。

2 个答案:

答案 0 :(得分:5)

第一个RMarkdown块在幕后调用rats <- rstan::stan_model(model_code=the_text),所以为了从后验分布中取样,你需要最终做rats_fit <- sampling(rats, data = list()),其余的参数与{{1}几乎相同}}。但是你必须先调用stan

答案 1 :(得分:1)

谢谢!在你的帮助下,我能够提出以下

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    String isbn;
    String ans;
    ArrayList<String> isbns = new ArrayList<String>();

   // user will enter at least 1 ISBN
    do{
    //Get the ISBN
    System.out.println("Enter an ISBN number ");
    isbns.add(input.nextLine());
    //loops till answer is yes or no
    while(true){
    System.out.println("Would you like to add another ISBN?");
    ans = input.nextLine();
    if(ans.equalsIgnoreCase("no"))
      break;
    else if (!(ans.equalsIgnoreCase("yes"))
      System.out.println("Please say Yes or No");
    }
    }while(!(ans.equalsIgnoreCase("yes"));
    input.close();

    //Strip out the spaces/System.out.println("Press 1 to enter a list of ISBN numbers to verify. ");System.out.println("Press 1 to enter a list of ISBN numbers to verify. ");dashes by replacing with empty character.
    for(int i = 0; i<isbns.size(); i++)
       isbns.set(i, isbns.get(i).replaceAll("( |-)", ""));
    isbn = isbn.replaceAll("( |-)", "");

    //Check depending on length. 
    boolean isValid = false;
   for(String isbn : isbns){
    if(isbn.length()== 10){
        isValid = CheckISBN10(isbn);
        print(isbn, isValid);
    }else if (isbn.length()== 13){
        isValid = CheckISBN13(isbn);
        print(isbn, isValid);
    }else{
        isValid = false;
        print(isbn, isValid);
    }
    }

public static void print(String isbn, boolean isValid){ 
    if(isValid){ 
        System.out.println(isbn + " IS a valid ISBN"); 
    }else{ System.out.println(isbn + " IS NOT a valid ISBN"); 
    }
}

//Checking ISBN-10 numbers are valid
private static boolean CheckISBN10(String isbn){
    int sum = 0; 
    String dStr;
    for (int d = 0; d < 10; d++){
        dStr = isbn.substring(d, d + 1);
        if (d < 9 || dStr != "X"){
            sum += Integer.parseInt(dStr) * (10-d);
        }else {
            sum += 10;
        }
    }
    return (sum %11 == 10);
}

private static boolean CheckISBN13(String isbn){
    int sum = 0; 
    int dVal;
    for (int d = 0; d < 13; d++){
        dVal = Integer.parseInt(isbn.substring(d, d + 1));
        if (d % 2 == 0){
            sum += dVal * 1;
        }else {
            sum += dVal * 3;
        }
    }
    return (sum % 10 == 0);

}