stan模型中的发电量块

问题描述

我正在建立一个标准的线性回归模型,我想包含generated quantities块,并且我想使用dot_self()函数。问题是我无法获得仿真样本。错误是:Stan model 'LinearRegression' does not contain samples.。我认为函数dot_self()未被识别为一个函数。 我在这里显示stan代码和R代码。 预先感谢。

注意:由于没有generated quantities块的模型可以正常工作,因此我确定输入的数据是正确的。

Stan代码:

data {          
  int<lower=1> N;           
  int<lower=1> K;           
  matrix[N,K] X;           
  vector[N] y;              
}
parameters {                
  vector[K] beta;           
  real<lower=0> sigma;      
}
model{                     
  vector[N] mu;             
  mu = X * beta;             

  beta ~ normal(0,10);
  sigma ~ cauchy(0,5);                                     
  y ~ normal(mu,sigma);    
}

generated quantities {
  real rss;                
  real totalss;
  real<lower=0,upper=1> R2;                 
  vector[N] mu;
  mu=X * beta;
  rss=dot_self(y-mu);
  totalss=dot_self(y-mean(y));
  R2=1 - rss/totalss;
}

运行Stan模型的R代码:

library(rstan)
library(coda)
library(ggplot2)
rstan_options(auto_write=T)
options(mc.cores=parallel::detectCores())

dat=list(N=N,K=ncol(X),y=y,X=X)
fit3 = stan(file = "C:.... LinearRegression.stan",data = dat,iter = 100,chains = 4)

print(fit3,digits=3,prob=c(.025,.5,.975))

解决方法

请务必添加您使用的数据(X 和 y)。

该错误是由于 R2 的边界造成的。我想没有必要对生成的数量施加限制。

去掉边界后的结果是:

enter image description here

X 和 y 模拟如下:

<UserJourney Id="SignUpOrSignIn">
  <OrchestrationSteps>
  
    <OrchestrationStep Order="1" Type="CombinedSignInAndSignUp" ContentDefinitionReferenceId="api.signuporsignin">
      <ClaimsProviderSelections>
        <ClaimsProviderSelection TargetClaimsExchangeId="GoogleExchange" />
        <ClaimsProviderSelection TargetClaimsExchangeId="MyCustomExchange" />
        <ClaimsProviderSelection ValidationClaimsExchangeId="LocalAccountSigninEmailExchange" />
      </ClaimsProviderSelections>
      <ClaimsExchanges>
        <ClaimsExchange Id="LocalAccountSigninEmailExchange" TechnicalProfileReferenceId="SelfAsserted-LocalAccountSignin-Email" />
      </ClaimsExchanges>
    </OrchestrationStep>

    <!-- Check if the user has selected to sign in using one of the social providers -->
    <OrchestrationStep Order="2" Type="ClaimsExchange">
      <Preconditions>
        <Precondition Type="ClaimsExist" ExecuteActionsIf="true">
          <Value>objectId</Value>
          <Action>SkipThisOrchestrationStep</Action>
        </Precondition>
      </Preconditions>
      <ClaimsExchanges>
        <ClaimsExchange Id="GoogleExchange" TechnicalProfileReferenceId="Google-OAuth2" />
        <ClaimsExchange Id="MyCustomExchange" TechnicalProfileReferenceId="Google-OAuth2" />
      </ClaimsExchanges>
    </OrchestrationStep>
    ...
  </UserJourney>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...