Q: how to run panel var regression model with categorical independent variables? This is my dataset:




CUSTOMER_ID
YEAR_MONTH
GENDER
LEGAL
AGE
AMOUNT_SPENT




1
202001
MALE
0
29
1500


1
202002
MALE
0
29
1700


...
...
...
...
...



2
202001
FEMALE
1
43
75000


2
202002
FEMALE
1
43
745000


...
...
...
...
...



3
202001
MALE
0
50
1800


3
202002
MALE
0
50
1950


...
...
...
...
...





gender and legal fields are categorical and it seems next month's amount spent depends on previous months' values. it seems the best model to describe my model is panel var.
the summerized  panel var model in R package is like this:
y(i,t) = μ(i) + ∑A(yi,t-l) + Bx(i,t) + Cs(i,t)+ ε(i,t)
But all the examples are for continuous variables but all my independent variables are categorical.
Do you know how to run this regression in R?

A: I would start by re-arranging the data so that you have a single row per CUSTOMER_ID. You will want to define your model response - AMOUNT_SPENT - to have a single value per row. You will also need to spread the previous month's values of AMOUNT_SPENT into new columns, for use as predictors - how many previous months values are useful for predicting the next month's value? Is it just one, or more? The dcast function from the data.table package is a potential way to spread the AMOUNT_SPENT column. You could call these something like AMOUNT_SPENT_1MONTH_AGO, AMOUNT_SPENT_2MONTH_AGO etc...
Then you can use a simple linear regression (or really any type of predictive model - random forest, kNN etc...). e.g.
model <- lm(AMOUNT_SPENT ~ GENDER + LEGAL + AGE + AMOUNT_SPENT_1MONTH_AGO, data=dataset)

If your aim is to build a predictive model I don't think the suggestion of a linear mixed model will help, as presumably you will be predicting for YEAR_MONTH's in the future, so including this as a predictor isn't useful.
