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)
library(readr)

data <- read_csv("./data/data1.csv",col_types = cols())
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

Thank you for your reply.I haven’t tried that yet.
Thanks to you, I was able to achieve my goal.
The coefficients of all explanatory variables were made positive!

With this method, the coefficients of some explanatory variables became negative values, but I was able to achieve the purpose with the method that the previous person taught me.
Thank you very much for your kind reply.

you’re right - that was a total brain fart on my part. glad you got it working.