How to control for categorical variable in regression? I'm trying to analyze two negatively-correlated variables, A and B (where A is the independent variable) while somehow taking into account a categorical variable C, with the intention of highlighting data that deviates above expected values.
For example, in the following subset of my data:
#, A, B, C
1, 14, 55, "X"
2, 12, 75, "X"
3, 10, 65, "X"
4, 14, 40, "Y"
5, 12, 30, "Y"
6, 10, 35, "Y"

Average:
A, B
14, 55
12, 60
10, 65    

I'd like to be able to highlight data point 2 because it deviates above the average value, but I'd also like to highlight data point 4, because although it deviates below the average value, it deviates above the expected value within its category.
I know how to do a simple linear regression on A and B, but I don't know how to account for the categorical variable.
 A: Based on your example provided, the qualitative variable "C" only has two levels, or possible values, we can incorporate it into a regression model by creating an indicator or dummy variable that takes on two possible numerical values.  
Having that,
$x_{1} = A, x_{2} = B$, and $x_{3} = C$, for linear regression,
$$y = \beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2} + \beta_{3}x_{3} + \epsilon_{i}$$
$$\therefore y = \beta_{0} + \beta_{1}A + \beta_{2}B + \beta_{3}C + \epsilon_{i}$$
From the variable "C", we can create a new variable that takes the form,  
$$\ C = \left\{ 
  \begin{array}{l l}
    1 & \quad \text{if $ith$ is X}\\
    0 & \quad \text{if $ith$ is Y}
  \end{array} \right.\\$$
and use this variable as a predictor in the regression equation. This results in the model
$$\ y_{i} = \beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2} + \beta_{3}x_{3} + \epsilon_{i} = \left\{ 
  \begin{array}{l l}
    \beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2} + \beta_{3}x_{3} + \epsilon_{i} & \quad \text{if $ith$ is X}\\
    \beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2} + \epsilon_{i} & \quad \text{if $ith$ is Y}
  \end{array} \right.\\$$
Now $\beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2}$ can be interpreted as Y, while $\beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2} + \beta_{3}x_{3}$ is interpreted as X.
A: To use them in a linear regression, you need to select a base category and create a variable for all other categories. So, in your example, you could create a dummy variable for 'B', which is equal to 1 when the category is 'B' and 0 otherwise. The coefficient on that variable will be the difference between when the category is 'A' and 'B'.  You might also wish to create interactions with the other variables to see if there is a slope effect as well as a level effect of the different categories.
A: You can:


*

*make different regressions, for each value of "C"

*OR change "C" into dummy variables and perform regression on all variables.

A: A standard method is to make a variable (called dummy variable) and code it 1 for either "Y" or "X" than regress on that variable.  For example D = 1 if C = "Y" and 0 otherwise. 
So you will estimate $A =\beta_0 + \beta_1B + \beta_2D + \epsilon$
In case you use R: 

D <- as.numeric(C == "Y")
lm(A ~ B + D)

