How to concatenate a double array and a categorical array?

46 views (last 30 days)
Hi,
I was trying to run a binary logistic regression using the function fitglm(Tf{:,3:11},Tf.Var2,'Distribution','binomial'). However, it returns me the error "Unable to concatenate a double array and a categorical array." The reason for this is that Tf{:,4} is the categorical variable gender whereas other variables are double. I am wondering how to include both numeric and catogorical variables as predictors in this logistic regression model?

Accepted Answer

Star Strider
Star Strider on 12 Mar 2022
The way I read the documentation for using a table (the tbl section), and
  • By default, fitglm takes the last variable as the response variable and the others as the predictor variables.
it might be necessary to create a new table by changing the order of the variables, for example:
Tf2 = table(TF{:,3:11},TF.Var2)
and then use ‘Tf2’ as the argument table.
Without the actual ‘Tf’ to experiment with, this is the best I can do.
  3 Comments
Star Strider
Star Strider on 12 Mar 2022
As always, my plesaure!
I do not understand what ‘Nuisance variables’ are. If they are Dummy variables, then Automatic Creation of Dummy Variables and specifically Dummy Variables with Reference Group would likely apply.
I do not have much experience with regression applications that create ‘dummy variables’. It appears that they are created as numeric equivalents to categorical variables, and that would appear to me to be appropriate. It may not be possible to prevent their appearing.
Peter Perkins
Peter Perkins on 14 Mar 2022
Weixi, I strongly recommend that you use the table itself as the input to fitglm, i.e. this syntax:
GLM = fitglm(TBL,MODELSPEC)
which is what SS is suggesting. Tht way you do not need to concatenate your categorical with the numeric vars, or "convert" the categorical to numeric to do so.
However:
fit = fitglm(TBL,<model>,"ResponseVariable",2)
will do it without needing to rearrange the variables. I don't know what your model specification would be. In my world, "controlling for nuisance variables" might mean including them as predictors. The doc will show how to do that.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!