vbs编程代码大全(vbs代码编写软件)
20个常用的计量经济学R & Stata命令对比汇总
1、 导入csv数据文档
Stata:insheet using "auto.csv"
R:mydata <- read.csv("auto.csv")
2、改变定义路径
Stata:cd "mydirectory"
R:setwd("mydirectory")
3、打印 "hello world"
Stata:di "Hello World"
R:print("Hello World")
4、列出前五行变量数据
Stata:list in 1/5
R:head(mydata) 或者 mydata[1:5,]
5、导入/使用数据
Stata:use "mydata.dta", clear
R:load("mydata.Rdata")
6、保存并且替换数据
Stata:save "mydata.dta", replace
R:save.image("mydata.Rdata")
7、根据变量X进行排序
Stata:sort x y
R:mydata[order(mydata$x, mydata$y),]
8、相关分析
Stata:cor x y
R:cor(x,y)
9、帮助命令
展开全文
Stata:help command
R:help(command)
10、打开数据
Stata:edit
R:edit(mydata)
11、列联表
Stata:table x y
R:table(mydata$x,mydata$y)
12、描述分析
Stata:summarize
R:summary(mydata)
13、logit回归
Stata:logit y x
R:summary(glm(y~x,data=mydata,family="binomial"))
14、Probit回归
Stata:probit y x
R:summary(glm(y~x,data=mydata,family=binomial(link = "probit")))
15、回归分析
Stata:reg y x1 x2
R:summary(lm(y~x1+x2, data=mydata))
16、回归分析不带常数项
Stata:reg y x1 x2, nocon
R:summary(lm(y~x1+x2-1, data=mydata))
17、回归分析带if选项
Stata:reg y x if (x>0)
18、直方图
Stata:hist x
R:Stata:hist(mydata$x)
19、散点图
Stata:scatter x y
R:plot x y
20、列出所有数据
Stata:list
R:mydata
第一部分:普林斯顿大学:R-Stata数据探索入门
来源:http://www.princeton.edu/~otorres/
第二部分:Stata++R命令对比表
来源:https://github.com/EconometricsBySimulation/RStata/
下表提供了Stata命令到R的一些快速转换,因为R支持多个数据集,所以在使用数据访问/修改命令时,我们需要指定要操作的特定数据集。我们使用mydata作为目标的默认数据集。
Stata
R
Deion
cls
cat("\014") -OR- cat(rep("\n",50)) -OR- ctrl+L
Clears Stata output / R console
clear all
rm(list=ls)
Clears data, value labels, etc from memory
insheet using "foo.csv", comma names
mydata <- read.csv("foo.csv", header=TRUE)
Read csv file
cd "mydirectory"
setwd("mydirectory")
Change working directories
pwd
getwd
Display the working directory
reg y x1 x2
summary(lm(y~x1+x2, data=mydata))
Ordinary least squares with constant
reg y x1 x2, nocon
summary(lm(y~x1+x2-1, data=mydata))
Ordinary least squares without constant
if (x==y) {...}
if (x==y) {...}
Initial line condition use to evaluate whether a command(s) should be exectuted
reg y x if (x>0)
lm(y~x, data=subset(mydata,x>0))
Select a conditional subset of data
forvalues i=1/100 {...}
for (i in 1:100) {...}
Loop through integer values of i from 1 to 100
foreach i in "a" "b" "c" {...}
for (i in c("a","b","c")) {...}
Loop through a list of items
di "Hello World"
print("Hello World")
Prints "hello world" on screen
do "mydofile.do"
source("myR.R")
Call and run code file
use "mydata.dta", clear
load("mydata.Rdata")
Load saved workspace/data
save "mydata.dta", replace
save.image("mydata.Rdata")
Save current workspace/data
di 2345^2
2345^2
Calculate 2345 squared
logit y x
summary(glm(y~x,data=mydata,family="binomial"))
Perform logit maximum likelihood estimation
probit y x
summary(glm(y~x,data=mydata,family=binomial(link = "probit")))
Perform probit maximum likelihood estimation
sort x y
mydata[order(mydata y),]
Sort the data frame by variable x
cor x y
cor(x,y)
Produce a table of correlates between x and y
help command
1. ?command 2. help(command)
Load the help file on a command
edit
edit(mydata)
Open data editor window (not recommended)
browse
View(mydata)
Visually inspect the dataset
summarize
summary(mydata)
Provide summary values for data
table x y
table(mydata y) # 1. ftable(y~x,data=mydata) # 2.
Two way table
hist x
hist(mydata$x)
Histogram of variable x
scatter x y
plot x y
Scatter plot of x on y
list
mydata
Print to screen all of the values of the data frame
list in 1/5
1. head(mydata) 2. mydata[1:5,]
Print to screen first 5 rows of data
generate x2=x^2
mydata x^2
Create a new variable x2 which is the square of x
replace x=y1+y2
1. mydata y1 + mydata x <- with(mydata, y1 + y2)
Change the x value of data to be equal to y1+y2
for i=1/10 { di `i' }
for (i in 1:10) print(i)
Print count from 1 to 10
replace x=0 if x<0
mydata x<0] <- 0
Replace all values of x less than 0 with zero
drop if x>100
mydata <- subset(mydata,!x>100)
Drop observations with x greater than 100
keep if x<100
mydata <- subset(mydata,x<100)
Keep observations with x less than 100
drop x
mydata$x <- NULL
Drop variable x from the data
keep x
mydata <- mydata$x
Keep only x in the data
append using "mydata2.dta"
mydata <- rbind(mydata, mydata2)
Append mydata2 to mydata
merge 1:1 index using "mydata2.dta"
merge(mydata,mydata2,index)
Merge two data sets together by index variable(s)
set obs 1000 gen x=rnormal
mydata$x <- rnorm(1000)
Generate 1000 random normal draws
set obs 1000 gen x=runiform
mydata$x <- runif(1000)
Generate 1000 random uniform draws
set obs 1000 gen x=rbinomial(10,.1)
mydata$x <- rbinom(1000, 10, .1)
Generate 1000 random binomial (10,.1) draws
count
nrow(mydata)
Count the number of observations in the data
foreach v of varlist * { rename v' v'old }
names(mydata) <- paste0(names(mydata),"old")
Rename all of the variables in the data ...old
rename oldvar newvar
colnames(dataframe)[colnames(dataframe)=="oldvar"] <- "newvar"
Rename variable.
clear set obs 100 gen x=rnormal(100) gen y=x*2 + rnormal(100)*5
mydata<-data.frame(x=x<-rnorm(100), y=x*2 + rnorm(100)*5)
Simulate a new data set with y dependent upon x
egen id = group(x y)
1. within(mydata, {ID <- ave(ID, list(x, y), FUN=seq_along)}) 2. mydata ID <- ave(ID, list(mydata y), FUN=seq_along)
Create an identifier ID from variables x and y
egen smallest = min(x y)
mydata %>% rowwise %>% mutate(smallest = min(c(x, y)))
For each row, find the smallest value within a set of columns
egen newvar = fcn(x y)
mydata %>% rowwise %>% mutate(newvar = fcn(c(x, y)))
For each row, apply a function to variables