I am reading a book and following along with examples. Currently, I am looking at a problem involving this data
age weight gender
<dbl> <dbl> <chr>
1 39 2817 Female
2 40 2935 Female
3 40 3231 Female
4 36 2729 Female
5 38 2754 Female
6 40 3421 Male
7 37 2539 Female
8 37 2847 Male
9 38 3176 Male
10 37 2628 Male
The book talks about analytically fitting a model with a linear model with different intercepts for gender, but the same regression coefficient. More precicely, it wants to fit
$$ \text{weight}_j = \alpha_j + \beta x_{ij} $$
Here $j = 0,1$ depending on if the weight belongs to a male or female.
Is there a way to do this in R? I'm not sure how to code an interaction term for an intercept.
答案 0 :(得分:1)
Two intercepts is equivalent to a true intercept and a binary variable.
All you have to do is say that $\text{gender}$ is a binary variable: 0 for male, 1 for female. You will fit the model: $$\text{weight}=\alpha+\beta_1\text{gender}+\beta_2\text{age}$$
Then $\alpha$ is the intercept for male, $\alpha+\beta_1$ is the intercept for female.