# Model overall effect of predictor within categories

I'm trying to fit a generalized linear model in R, but am quite new to regression, and struggling to work out how to have predictors nested within categorical variables.

An example of my data:

Response   Predictor   Category
1          1.22           A
5          5.67           A
3          4.52           B
3          7.23           B
9          2.75           C
4          1.11           C


etc....

I want to test for an effect of the Predictor on the Response, within each Category. I have been able to test within each Category by sub-setting the data into each Category, but then I get lots of different test results. I thought there is probably a way to do an overall test, but I can't figure out what formula I'd use.

You didn't tell us a lot about your data and which generalized linear model you want to fit, but the main question seems to be about how to model the linear predictor as a function of the covariates. Since the response seems to be a count, I will write example code for a poisson regression (in R, and poisson regression by default uses a log link function). So

out1  <-  glm(Response ~ Category + Predictor, family=poisson, data=your.data.frame)


where your.data.frame is a data frame containing the variables, Response as numerical variable, Predictor as numeric, while Category must be coded as factor. The code above estimates a simple model with separate intercepts for A, B and C, and a common slope for Predictor.

To code a model with separate slopes for A,B and C we need to include an interaction, which you can do as

out2  <-  glm(Response ~ Category * Predictor, family=poisson, data=your.data.frame)


the only change to the call is that a + was replaced with a *, which in R is the crossing operator, which expands to Category + Predictor + Category:Predictor, adding the interactions.