regression - How does Stata treat multiple factor variables in regression?
问题描述
I have a city-year level dataset, and run the follow regression with city fixed effects:
reg y x i.city
I think this is equivalent to generating a dummy variable for each of 300 cities in the data, and run (city 1 as base level):
reg y x city2 ... city300
However, I need to include year dummies as well. I get the estimates using:
reg y x i.city i.year
Does anyone know what is going behind this regression in matrix form? Is that the same as generating one dummy for each year and run the following?
reg y x city2 ... city300 year2 ... year20
The reason I want to do this is try to code the command from scratch using matrix operations (X'X)^{-1}(X'y), where X includes the city dummies and year dummies.
解决方案
您正在使用的称为虚拟(0,1)变量的角点编码,其中 k-1 二进制(0,1)变量级别用于每个因子(分类变量)。如果您指定不应使用常数项:
reg y x i.city i.year, nocon
然后零和约束编码将用于二进制变量构造,其中将有一个用于 X 矩阵中的 city1 和 year1 的二进制变量。
如您所见(下图),当饮食中的视黄醇浓度 (retdiet) 回归male
虚拟变量时,常数 (y-intcp) 的系数项是女性 (815) 中的平均值 y,系数为male
delta在男性和女性之间的 y 值。然而,当使用两个虚拟指标时 -fem
和male
, 和, nocon
被指定(在逗号之后),回归系数的值fem
和male
是每组中 y (retdiet)的平均值。
推荐阅读
- python-3.x - Python说我只有两个值,需要三个,而已经有三个值
- jquery - 单击打开 div 的按钮后,如何阻止我的固定位置“覆盖”div 滚动到页面顶部?
- rpm - 如何安装 pgbadger:我正面临这个问题
- python - Python:将列表中项目的前n个字符与同一列表中所有其他项目的前n个字符进行比较
- python - Python 启动案例
- java - drawMatches() 抛出 CvException:不支持的目标图像
- awk - 如何使用文件中的排除模式
- css - 在不同页面上覆盖 React Big Calendar CSS
- ios - 我的代码中不断出现错误,特别是“类型 'FirstViewController' 没有成员”和“使用未解析的标识符”
- r - 如何修复 SVM 中的“不一致数组”错误?