1

The dependent variable Value of the data frame DF is predicted using the independent variables Mean, X, Y in the following way:

DF <- DF %>% 
    group_by(Country, Sex) %>%
    do({ 
        mod = lm(Value ~ Mean + X + Y, data = .) 
        A <- predict(mod, .)
        data.frame(., A)
    })

Data are grouped by Country and Sex. So, the fitting formula can be expressed as:

Value(Country, Sex) = a0(Country, Sex) + a1(Country, Sex) Mean + a2(Country, Sex) X + a3(Country, Sex) Y

However, I want to use this formula:

Value(Country, Sex) = a0(Country, Sex) + a1(Country, Sex) Mean + a2(Country) X + a3(Country) Y

Where a2 and a3 are independent of Sex. How can I do it?

1 Answer 1

2

I don't think you can when grouping by Country and Sex. You could just group by Country and add interactions with Sex:

DF <- DF %>% 
group_by(Country) %>%
do({ 
    mod = lm(Value ~ Sex + Mean*Sex + X + Y, data = .) 
    A <- predict(mod, .)
    data.frame(., A)
})

or estimate your model in one go adding interactions with Sex and Country:

mod <- lm(Value ~ Sex*Country*Mean + Country*X + Country*Y
A <- predict(mod)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.