# I want to limit the effect of variables in regression analysis to positive values

#### Summary:

I want to limit the effect of variables in regression analysis to positive values.

#### Description:

I am analyzing the impact of multiple advertisements on sales by regression analysis.
I want to make it a prerequisite that advertising has a positive impact on sales.
How can I enter Stan’s code to limit the variable’s influence to positive values?

#### Reproducible Steps: & Current Output:

I typed “parameters{ real<lower=0> p;}”,
and I have tried several prior distribution settings.
(for example “model{p~normal(0.5,0.2);”)

#### Reproducible Steps: & Current Output:

Some variables have negative influence.

#### Expected Output:

I want to limit the effect of variables in regression analysis to positive values.

#### Current Version:

v2.32.2

Positivity constraints via `<lower=0>` work. To troubleshoot further, we would need to see your entire model and a complete description of which parameters you want to constrain to be positive.

Thank you for your reply. I would like to share my stan model below.
I want the explanatory variable"X" to have only a positive influence on the target variable"Y".
In other words, I want the coefficient of the explanatory variable"X" to always be a positive value.

``````library(rstan)
library(ggplot2)

data <- data[!is.na(data\$y), ]

y <- as.numeric(data[[ncol(data)]])
**X <- as.matrix(data[, -ncol(data)])**

data_list <- list(
N = nrow(data),
K = ncol(data) - 1,
X = X,
y = y
)

stan_code <- "
data {
int<lower=0> N;
int<lower=0> K;
matrix[N, K] X;
vector[N] y;
}
parameters {
real beta0;
vector[K] beta;
real<lower=0> sigma;
}
model {
vector[N] mu;
beta ~ normal(0, 1);  //
mu = beta0 + X * beta;
y ~ normal(mu, sigma);
}
"

model <- stan_model(model_code = stan_code)

fit <- sampling(model, data = data_list, iter = 2000, chains = 4)

samples <- as.data.frame(fit)

coef_summary <- summary(fit)\$summary

coef_summary <- coef_summary[2:12,]
coef_data <- data.frame(
variable = colnames(X),
mean = coef_summary[, 1],
lower = coef_summary[, 4],
upper = coef_summary[, 5]
)
``````

Have you tried `vector<lower=0>[K] beta;` ?

2 Likes