Stata Talk @ Shandong University as TA

Stata Talk @ Shandong University as TA

Actually, it’s a bit boring. This is the third time I’ve talked about Stata as TA… The only difference is that this time it is for graduate students, with only 10 listeners. Following pictures are the screenshots of my slide.

Originally, I wanted to write a detailed handout, but then I felt bored to write any handout, so there was no handout, you can find previous handouts on my website > Downloads.






























You can browse the online version of the slide using a computer browser: A Brief Introduction to Stata. To get a PDF version, check A Brief Introduction to Stata / PDF version.

Actually, this afternoon, I just explained the first 18 pages of this slide. Here are some Stata codes I wrote during my presentation.

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 清空内存 */
* 清楚所有数据、变量
clear all // 清空内存
/* 查看当前工作目录的位置 */
pwd
/* 设定工作目录为 ~/Desktop/dataset */
cd ~/Desktop/dataset

/* 调用系统数据集 */
sysuse auto, clear
/* 保存为 auto.dta 数据 */
save auto, replace

/* 一定要设定好工作目录,然后把所有的数据文件放在工作目录下 */
use auto, clear
cd ~/Desktop
use auto, clear
use ~/Desktop/dataset/auto, clear

clear all
pwd
cd ~/Desktop
sysuse auto, clear
save auto, replace

use auto, clear

/* copy */
/* 把 ~/Desktop/dataset/auto.dta 拷贝到 ~/Desktop/auto2.dta */
copy ~/Desktop/dataset/auto.dta ~/Desktop/auto2.dta, replace
copy "https://czxb.github.io/mr/pingan.csv" "pingan2.csv", replace
copy "http://data.eastmoney.com/cjsj/pmi.html" temp.txt, replace

/* 导入csv格式的数据 */
import delimited using "pinga n2.csv", clear
save pingan, replace
erase "auto.dta"
export delimited using "pingan3.csv", replace

copy "https://czxb.github.io/mr/grilic_small.xls" ., replace
import excel using grilic_small.xls, clear firstrow

infix strL v 1-20000 using temp.txt, clear

clear
unicode encoding set gb18030
unicode translate temp.txt
unicode erasebackups, badidea

/* 数据管理 */
sysuse auto, clear
describe
des
list price in 1/5
list in 1/5
list if _n <= 5
generate id = _n
order id
gen total = _N
order total
list price weight, noobs
list mpg weight if mpg > 20
list mpg weight foreign if !foreign
list mpg weight foreign if ~foreign
list mp wei foreign if mpg > 20 & !foreign | weight > 4000
list mpg if mpg <= 40 & mpg => 20
list mpg if inrange(mpg, 20, 40)
list mpg if inlist(make, "AMC Concord", "Buick LeSabre", "Chev. Impala")
gsort price
gsort -price
gsort rep78 price
sort price

codebook price
keep price
gen dum = (price > 3000)
replace dum = 0 if price < 5000
gen price2 = price^2
gen total = sum(price)
egen total2 = sum(price)
egen total3 = total(price)

sysuse auto, clear
keep price rep78
bysort rep78: egen mean = mean(price)
bysort rep78: egen std = sd(price)
egen rowmax = rowmax(price-mean)
gen temp = price if rep78 == 1
gen temp2 = price in 1/2
replace temp2 = price^2 if rep78 != 1

rename price price2

keep if rep78 == 1

sysuse auto, clear
keep price rep78
keep if rep78 == 3 & price >= 5000
drop if rep78 != 3 | price < 5000

sysuse auto, clear
su price
return list
display r(mean)
display r(mean) + 1000
display in yellow r(mean)
display in green r(mean)
display in white r(mean)
display in red r(mean)
display as input r(mean)
display as error r(mean)
display as text r(mean)
display as result r(mean)
di 1 + 1
di 4^2

hist price, bin(30) freq
help twoway bar
graph bar price, over(rep78) over(for) missing
graph bar price, over(rep78) missing

tabulate rep78, summarize(price)
clear
input rep78 mean
1 4564.5
2 5967.625
3 6429.233
4 6071.5
5 5913
end

twoway || ///
scatter mean rep78 || ///
bar mean rep78 || ///
line mean rep78, xtitle("1978年汽车修理的次数") ///
ytitle("平均价格") ///
yla(4000(1000)7000, format(%6.2f)) ///
title("汽车价格与修理次数的关系") ///
subtitle("2019年9月9日") ///
caption("数据来源:Stata") ///
leg(order(1 "散点图" 2 "柱形图" 3 "线图") pos(11) row(2) ring(0))

sysuse auto, clear
gr pie price, over(rep78) missing sch(uncluttered)
gr export pie.svg, replace
gr export pie.png, replace
gr export pie.pdf, replace

sysuse auto, clear
reg price weight
tw sc price weight || ///
lfit price weight, xla(0(1000)5000) lp(*40) || ///
function 2.044063 * x - 6.707553, range(0 5000) lc(red)
# Stata

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×