Stata 为asclogit和nlogit创建表

Stata 为asclogit和nlogit创建表,stata,Stata,假设我有下表: id | car | sex | income ------------------------------- 1 | European | Male | 45000 2 | Japanese | Female | 48000 3 | American | Male | 53000 我想创建以下一个: | id | car | choice | sex | income -----------------------------

假设我有下表:

id | car      | sex    | income
-------------------------------
1  | European | Male   | 45000
2  | Japanese | Female | 48000
3  | American | Male   | 53000
我想创建以下一个:

  | id | car      | choice | sex    | income
--------------------------------------------
1.| 1  | European | 1      | Male   | 45000
2.| 1  | American | 0      | Male   | 45000
3.| 1  | Japanese | 0      | Male   | 45000
  | ----------------------------------------
4.| 2  | European | 0      | Female | 48000
5.| 2  | American | 0      | Female | 48000
6.| 2  | Japanese | 1      | Female | 48000
  | ----------------------------------------
7.| 3  | European | 0      | Male   | 53000
8.| 3  | American | 1      | Male   | 53000
9.| 3  | Japanese | 0      | Male   | 53000

我想安装一个
asclogit
,根据Stata手册中的说明,此表格格式似乎是必要的。但是,我还没有找到一种简单的方法来创建它。

您可以使用
cross
命令来生成所有可能的组合:

clear

input byte id str10 car str8 sex long income
1 "European" "Male"   45000
2 "Japanese" "Female" 48000
3 "American" "Male"   53000
end

generate choice = 0
save old, replace

keep id
save new, replace

use old
rename id =_0
cross using new

replace choice = 1 if id_0 == id
replace sex = cond(id == 2, "Female", "Male")
replace income = cond(id == 1, 45000, cond(id == 2, 48000, 53000))
请注意,此处使用的
cond()
函数相当于:

replace sex = "Male"   if id == 1
replace sex = "Female" if id == 2
replace sex = "Male"   if id == 3

replace income = 45000 if id == 1
replace income = 48000 if id == 2
replace income = 53000 if id == 3
上述截取的代码产生所需的输出:

drop id_0
order id car choice sex income
sort id car

list, sepby(id)

     +------------------------------------------+
     | id        car   choice      sex   income |
     |------------------------------------------|
  1. |  1   American        0     Male    45000 |
  2. |  1   European        1     Male    45000 |
  3. |  1   Japanese        0     Male    45000 |
     |------------------------------------------|
  4. |  2   American        0   Female    48000 |
  5. |  2   European        0   Female    48000 |
  6. |  2   Japanese        1   Female    48000 |
     |------------------------------------------|
  7. |  3   American        1     Male    53000 |
  8. |  3   European        0     Male    53000 |
  9. |  3   Japanese        0     Male    53000 |
     +------------------------------------------+

有关详细信息,请在Stata的命令提示符下键入
help cross
help cond()

您可以使用
cross
命令生成所有可能的组合:

clear

input byte id str10 car str8 sex long income
1 "European" "Male"   45000
2 "Japanese" "Female" 48000
3 "American" "Male"   53000
end

generate choice = 0
save old, replace

keep id
save new, replace

use old
rename id =_0
cross using new

replace choice = 1 if id_0 == id
replace sex = cond(id == 2, "Female", "Male")
replace income = cond(id == 1, 45000, cond(id == 2, 48000, 53000))
请注意,此处使用的
cond()
函数相当于:

replace sex = "Male"   if id == 1
replace sex = "Female" if id == 2
replace sex = "Male"   if id == 3

replace income = 45000 if id == 1
replace income = 48000 if id == 2
replace income = 53000 if id == 3
上述截取的代码产生所需的输出:

drop id_0
order id car choice sex income
sort id car

list, sepby(id)

     +------------------------------------------+
     | id        car   choice      sex   income |
     |------------------------------------------|
  1. |  1   American        0     Male    45000 |
  2. |  1   European        1     Male    45000 |
  3. |  1   Japanese        0     Male    45000 |
     |------------------------------------------|
  4. |  2   American        0   Female    48000 |
  5. |  2   European        0   Female    48000 |
  6. |  2   Japanese        1   Female    48000 |
     |------------------------------------------|
  7. |  3   American        1     Male    53000 |
  8. |  3   European        0     Male    53000 |
  9. |  3   Japanese        0     Male    53000 |
     +------------------------------------------+

有关详细信息,请在Stata的命令提示符下键入
help cross
help cond()

在Python中使用
Pandas
的临时解决方案如下:

1) 用以下方法打开底座:

df = pd.read_stata("mybase.dta")
2) 使用已接受答案的代码

3) 保存基础:

df.to_stata("newbase.dta")

在Python中使用
Pandas
的临时解决方案如下:

1) 用以下方法打开底座:

df = pd.read_stata("mybase.dta")
2) 使用已接受答案的代码

3) 保存基础:

df.to_stata("newbase.dta")

请参阅Stata中的
dataex
,了解如何生成在web论坛中有用的数据示例。(如有必要,首先使用
ssc安装dataex
进行安装)

这可能是使用
填充
然后再填充缺失的练习

* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id str10 car str8 sex long income
1 "European" "Male"   45000
2 "Japanese" "Female" 48000
3 "American" "Male"   53000
end

fillin id car

foreach v in sex income {
    bysort id (_fillin) : replace `v' = `v'[1]
}

list , sepby(id)

     +-------------------------------------------+
     | id        car      sex   income   _fillin |
     |-------------------------------------------|
  1. |  1   European     Male    45000         0 |
  2. |  1   American     Male    45000         1 |
  3. |  1   Japanese     Male    45000         1 |
     |-------------------------------------------|
  4. |  2   Japanese   Female    48000         0 |
  5. |  2   European   Female    48000         1 |
  6. |  2   American   Female    48000         1 |
     |-------------------------------------------|
  7. |  3   American     Male    53000         0 |
  8. |  3   European     Male    53000         1 |
  9. |  3   Japanese     Male    53000         1 |
     +-------------------------------------------+

请参阅Stata中的
dataex
,了解如何生成在web论坛中有用的数据示例。(如有必要,首先使用
ssc安装dataex
进行安装)

这可能是使用
填充
然后再填充缺失的练习

* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id str10 car str8 sex long income
1 "European" "Male"   45000
2 "Japanese" "Female" 48000
3 "American" "Male"   53000
end

fillin id car

foreach v in sex income {
    bysort id (_fillin) : replace `v' = `v'[1]
}

list , sepby(id)

     +-------------------------------------------+
     | id        car      sex   income   _fillin |
     |-------------------------------------------|
  1. |  1   European     Male    45000         0 |
  2. |  1   American     Male    45000         1 |
  3. |  1   Japanese     Male    45000         1 |
     |-------------------------------------------|
  4. |  2   Japanese   Female    48000         0 |
  5. |  2   European   Female    48000         1 |
  6. |  2   American   Female    48000         1 |
     |-------------------------------------------|
  7. |  3   American     Male    53000         0 |
  8. |  3   European     Male    53000         1 |
  9. |  3   Japanese     Male    53000         1 |
     +-------------------------------------------+

如果要使用虚拟变量,
重塑
也是一个选项

clear
input byte id str10 car str8 sex long income
1 "European" "Male"   45000
2 "Japanese" "Female" 48000
3 "American" "Male"   53000
end

tabulate car, gen(choice)
reshape long choice, i(id)

label define car 2 "European" 3 "Japanese" 1 "American"
drop car
rename _j car
label values car car

如果要使用虚拟变量,
重塑
也是一个选项

clear
input byte id str10 car str8 sex long income
1 "European" "Male"   45000
2 "Japanese" "Female" 48000
3 "American" "Male"   53000
end

tabulate car, gen(choice)
reshape long choice, i(id)

label define car 2 "European" 3 "Japanese" 1 "American"
drop car
rename _j car
label values car car