首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的STAN :线性回归的维数误差

R中的STAN :线性回归的维数误差
EN

Stack Overflow用户
提问于 2019-08-15 08:49:00
回答 1查看 931关注 0票数 2

下面,您可以找到一个简单的线性回归与2个预测。效果很好。但是,当只有一个预测器(请参见第二个脚本)时,将出现以下错误消息:

异常:在上下文中声明和发现的数字维不匹配;处理stage=data初始化;变量name=x;dims declared=(20,1);dims found=(20)

问题是,带有1行的矩阵会在向量中自动转换,因此与声明的维度不匹配。一种解决方案是将x声明为向量,但问题是,我运行的脚本具有不同数量的预测器(可能是1或更多)。

斯坦脚本:

代码语言:javascript
复制
write("// Stan model for simple linear regression
data {
int<lower=0> N;  // number of data items
int<lower=0> K;// number of predictors
matrix[N, K] x;// predictor matrix
vector[N] y;// outcome vector
}
parameters {
real alpha;       // intercept
vector[K] beta;       // coefficients for predictors
real<lower=0> sigma;  // error scale
}
model {
y ~ normal(x * beta + alpha, sigma);  // likelihood
}", "ex_dimension.stan")

带有两个预测器的r脚本(工作):

代码语言:javascript
复制
N=20
K=2
x1=1:N+rnorm(N,0,0.5)
x2=rnorm(N,2,1)
x=cbind(x1,x2)
a=2
b=3
y=a*x1+b*x2+rnorm(N,0,1)


stan_data=list(N=N,
               K=K,
               x=x,
               y=y)
fit <- stan(file = "ex_dimension.stan",
            data = stan_data,
            warmup = 500,
            iter = 2000,
            chains = 4,
            cores = 4,
            thin = 1,
            control=list(adapt_delta=0.8))
fit

带有一个预测器的脚本(不工作):

代码语言:javascript
复制
stan_data=list(N=N,
               K=1,
               x=x[,1],
               y=y)
fit <- stan(file = "ex_dimension.stan",
            data = stan_data,
            warmup = 500,
            iter = 2000,
            chains = 4,
            cores = 4,
            thin = 1,
            control=list(adapt_delta=0.8))
fit
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-15 10:44:20

drop = FALSE对矩阵进行子集,以避免将其折叠成向量,从而将错误的输入传递给Stan模型(参见高级R-细分章节)。

代码语言:javascript
复制
library(rstan)

stan_data <- list(N = N, K = 1, x = x[, 1, drop = FALSE], y = y)

fit <- stan(
    model_code = "// Stan model for simple linear regression
        data {
            int<lower=0> N;       // number of data items
            int<lower=0> K;       // number of predictors
            matrix[N, K] x;       // predictor matrix
            vector[N] y;          // outcome vector
        }
        parameters {
            real alpha;           // intercept
            vector[K] beta;       // coefficients for predictors
            real<lower=0> sigma;  // error scale
        }
        model {
            y ~ normal(x * beta + alpha, sigma);  // likelihood
        }",
    data = stan_data,
    chains = 1
)

fit
#> Inference for Stan model: 4f8ba0f0c644593f519910e9d2741995.
#> 1 chains, each with iter=2000; warmup=1000; thin=1; 
#> post-warmup draws per chain=1000, total post-warmup draws=1000.
#> 
#>           mean se_mean   sd   2.5%    25%    50%    75%  97.5% n_eff Rhat
#> alpha     6.26    0.06 1.20   3.93   5.49   6.25   7.04   8.68   470    1
#> beta[1]   2.00    0.00 0.10   1.81   1.94   2.00   2.06   2.19   453    1
#> sigma     2.70    0.02 0.50   1.87   2.35   2.62   2.97   3.88   458    1
#> lp__    -28.15    0.06 1.21 -31.12 -28.80 -27.84 -27.23 -26.74   366    1
#> 
#> Samples were drawn using NUTS(diag_e) at Thu Aug 15 12:41:19 2019.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at 
#> convergence, Rhat=1).
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57507123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档