Is there a way to merge multiple variable valuses into one?

41 Views Asked by At

I am doing emmeans analysis for my bachelors thesis. I am analysing households incomes based on different variables like education, number of children, job sector, area ect.

The question is: Is there a way to merge eg. multiple living areas into one, as they have basically the same mmean and then construct the GLM again?

In SAS I would use this code to merge 3 areas into one: ESTIMATE "BB-KE-ZA" intercept 3 REGION 1 1 0 0 0 0 1 /divisor=3;

and I wish to do the same in R.

I wish to create new value of the variable Region where will be merged 3 values of the variable Region that have same least square means.

1

There are 1 best solutions below

0
On

Sure. If you have something like

EMM <- emmeans(model, "group")

and group has levels A, B, C, D, E, and you want to average (B, D, and E) as well as (A and C), you could use

confint(contrast(EMM, list(avg1 = c(0,1,0,1,1)/3, avg2 = c(1,0,1,0,0)/2)))

While contrast() is primarily intended to compute contrasts, it can compute any linear function -- similar to your SAS ESTIMATE statement except it works with the EMMs rather than the regression coefficients.