专栏名称: 超超跃跃
“风吹枯叶落,落叶生肥土,肥土丰香果。<br...
目录
今天看啥  ›  专栏  ›  超超跃跃

stata tips(2)

超超跃跃  · 简书  ·  · 2021-03-29 23:46

文章预览

**缺失值处理注意事项

**缺失值大于已有的数值

list if age > 60 & age < .  (不会列出缺失值)

list if age >=.

**更有效地删除缺失值

用missing函数,不建议用drop if age == .

使用drop if missing(age)

clear

input x1 x2 x3 x4

1 2 . 4

4 5 .a 4

6 . .b 5

7 18 .c 9

end

数据

egen min = min(x3)   // 忽略了所有缺失值,也就是缺失值那一行删除

egen max = max(x2)  // 忽略了缺失值

egen rowmax = rowmax(x1 x2 x3)  // 计算每一行的最大值,忽略了缺失值

* rowtotal()、rowmean() 和 rowsd()函数会忽略缺失值

egen miss = rowmiss(a b c)  //得到a、b、c三个变量每行缺失值的个数

correlate x1 x2 x4  // 忽略含缺失值的观测值

correlate x1 x2 x4 if !missing(x2)  //与上命令结果等价

pwcorr x1 x2 x4   // 利用所有成对数据,与下面的命令不一样,x1和x4的相关系数不同

pwcorr x1 x2 x4 if !missing(x2)

把缺失值替换成数值

注意以下命令,会把缺失值.情况也修改

replace x2 =10 if x2 > 4

准确做法是replace x2 =10 if x2 > 4 & !missing(x2)

数据


replace x2 = 6 if x2 !=2(缺失值也会跟着替代)


数据

replace x2=0 if x2 ==.

mvencode x2, mv(. = 0)   //与上命令等价

x3这样的数列就非常麻烦,采用以下方法

mvencode x3, mv(. = 1 \ .a = 2)

把数值替换成缺失值

mvdecode x2, mv(18)

mvdecode x2, mv(2 = . \ 5 = .a)

控制变量缺失值剔除

*批量的方法

local vlist "var1 var2 var3"

egen miss = rmiss(`vlist')

drop if miss !=0    //删除上述变量中的缺漏值,有一个则都删除

………………………………

原文地址:访问原文地址
快照地址: 访问文章快照
总结与预览地址:访问总结与预览