- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 2590 个
- 通用积分
- 41.0599
- 学术水平
- 10 点
- 热心指数
- 17 点
- 信用等级
- 9 点
- 经验
- 991 点
- 帖子
- 44
- 精华
- 0
- 在线时间
- 369 小时
- 注册时间
- 2015-11-4
- 最后登录
- 2024-3-15
硕士生
还不是VIP/贵宾
- 威望
- 0 级
- 论坛币
- 2590 个
- 通用积分
- 41.0599
- 学术水平
- 10 点
- 热心指数
- 17 点
- 信用等级
- 9 点
- 经验
- 991 点
- 帖子
- 44
- 精华
- 0
- 在线时间
- 369 小时
- 注册时间
- 2015-11-4
- 最后登录
- 2024-3-15
| 开心 2020-5-24 00:57:03 |
---|
签到天数: 7 天 连续签到: 1 天 [LV.3]偶尔看看II
|
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
- ####分位数回归####
- library(quantreg) # 载入quantreg包
- data(engel) # 加载quantreg包自带的数据集
- ##单变量回归
- #分位数回归(tau = 0.5)
- fit1 = rq(foodexp ~ income, tau = 0.5, data = engel)
- r1 = resid(fit1) # 得到残差序列,并赋值为变量r1
- c1 = coef(fit1) # 得到模型的系数,并赋值给变量c1
- summary(fit1) # 显示分位数回归的模型和系数
- summary(fit1, se = "boot") # 通过设置参数se,可以得到系数的假设检验
- #分位数回归(tau = 0.5、0.75)
- fit1 = rq(foodexp ~ income, tau = 0.5, data = engel)
- fit2 = rq(foodexp ~ income, tau = 0.75, data = engel)
- #模型比较
- anova(fit1,fit2) #方差分析
- #画图比较分析
- plot(engel$foodexp , engel$income,pch=20, col = "#2E8B57",
- main = "家庭收入与食品支出的分位数回归",xlab="食品支出",ylab="家庭收入")
- lines(fitted(fit1), engel$income,lwd=2, col = "#EEEE00")
- lines(fitted(fit2), engel$income,lwd=2, col = "#EE6363")
- legend("topright", c("tau=.5","tau=.75"), lty=c(1,1),
- col=c("#EEEE00","#EE6363"))
- #不同分位点的回归比较
- fit = rq(foodexp ~ income, tau = c(0.05,0.25,0.5,0.75,0.95), data = engel)
- plot( summary(fit))
- ##多变量回归
- data(barro)
- fit1 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.25)
- fit2 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.50)
- fit3 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.75)
- # 替代方式 fit <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, method = "fn", tau = 1:4/5, data = barro)
- anova(fit1,fit2,fit3) # 不同分位点模型比较-方差分析
- anova(fit1,fit2,fit3,joint=FALSE)
- #不同分位点拟合曲线的比较
- plot(barro$y.net,pch=20, col = "#2E8B57",
- main = "不同分位点拟合曲线的比较")
- lines(fitted(fit1),lwd=2, col = "#FF00FF")
- lines(fitted(fit2),lwd=2, col = "#EEEE00")
- lines(fitted(fit3),lwd=2, col = "#EE6363")
- legend("topright", c("tau=.25","tau=.50","tau=.75"), lty=c(1,1),
- col=c( "#FF00FF","#EEEE00","#EE6363"))
- ##stata实例
- library(haven)
- grilic <- read_dta("G:/R_data/Project/Econometrics/stata learn/grilic.dta")
- grfm=lw~s+iq+expr+tenure+rns+smsa
- grolsfit=lm(grfm,data=grilic);summary(grolsfit) #basic OLS reg
- grrq0.5=rq(grfm,tau=0.5,data=grilic,model=TRUE) #中位数回归
- summary(grrq0.5);summary(grrq0.5, se = "boot")
- grrq0.1=rq(grfm,tau=0.1,data=grilic,model=TRUE) #0.25分位数回归
- summary(grrq0.1);summary(grrq0.1, se = "boot")
- grrq0.9=rq(grfm,tau=0.9,data=grilic,model=TRUE) #0.75分位数回归
- summary(grrq0.9);summary(grrq0.9, se = "boot")
- anova(grrq0.1,grrq0.5) #检验不同分位数回归估计值是否相同,零假设系数相同
- anova(grrq0.1,grrq0.5,grrq0.9) #比较不同分位数系数是否相同
- taus <- c(.05,.1,.25,.75,.9,.95)
- grrq=rq(grfm,tau=taus,data=grilic,model=TRUE) #同时回归多个分位数回归
- summary(grrq,se="boot", seed = 500000)
- plot(summary(grrq,se="boot", seed = 520))
- library(uqr)
- data(trust);View(trust)
- formula=Trust_in_the_ECB~Trust_in_the_EU+Trust_in_National_Government
- cre=~Trust_in_the_EU+Trust_in_National_Government
- rif=urq(formula,data=trust,cre=cre,id="countryname")
- rif$coefficients
复制代码- ####分位数回归####
- library(quantreg) # 载入quantreg包
- data(engel) # 加载quantreg包自带的数据集
- ##单变量回归
- #分位数回归(tau = 0.5)
- fit1 = rq(foodexp ~ income, tau = 0.5, data = engel)
- r1 = resid(fit1) # 得到残差序列,并赋值为变量r1
- c1 = coef(fit1) # 得到模型的系数,并赋值给变量c1
- summary(fit1) # 显示分位数回归的模型和系数
- summary(fit1, se = "boot") # 通过设置参数se,可以得到系数的假设检验
- #分位数回归(tau = 0.5、0.75)
- fit1 = rq(foodexp ~ income, tau = 0.5, data = engel)
- fit2 = rq(foodexp ~ income, tau = 0.75, data = engel)
- #模型比较
- anova(fit1,fit2) #方差分析
- #画图比较分析
- plot(engel$foodexp , engel$income,pch=20, col = "#2E8B57",
- main = "家庭收入与食品支出的分位数回归",xlab="食品支出",ylab="家庭收入")
- lines(fitted(fit1), engel$income,lwd=2, col = "#EEEE00")
- lines(fitted(fit2), engel$income,lwd=2, col = "#EE6363")
- legend("topright", c("tau=.5","tau=.75"), lty=c(1,1),
- col=c("#EEEE00","#EE6363"))
- #不同分位点的回归比较
- fit = rq(foodexp ~ income, tau = c(0.05,0.25,0.5,0.75,0.95), data = engel)
- plot( summary(fit))
- ##多变量回归
- data(barro)
- fit1 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.25)
- fit2 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.50)
- fit3 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.75)
- # 替代方式 fit <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, method = "fn", tau = 1:4/5, data = barro)
- anova(fit1,fit2,fit3) # 不同分位点模型比较-方差分析
- anova(fit1,fit2,fit3,joint=FALSE)
- #不同分位点拟合曲线的比较
- plot(barro$y.net,pch=20, col = "#2E8B57",
- main = "不同分位点拟合曲线的比较")
- lines(fitted(fit1),lwd=2, col = "#FF00FF")
- lines(fitted(fit2),lwd=2, col = "#EEEE00")
- lines(fitted(fit3),lwd=2, col = "#EE6363")
- legend("topright", c("tau=.25","tau=.50","tau=.75"), lty=c(1,1),
- col=c( "#FF00FF","#EEEE00","#EE6363"))
- ##stata实例
- library(haven)
- grilic <- read_dta("G:/R_data/Project/Econometrics/stata learn/grilic.dta")
- grfm=lw~s+iq+expr+tenure+rns+smsa
- grolsfit=lm(grfm,data=grilic);summary(grolsfit) #basic OLS reg
- grrq0.5=rq(grfm,tau=0.5,data=grilic,model=TRUE) #中位数回归
- summary(grrq0.5);summary(grrq0.5, se = "boot")
- grrq0.1=rq(grfm,tau=0.1,data=grilic,model=TRUE) #0.25分位数回归
- summary(grrq0.1);summary(grrq0.1, se = "boot")
- grrq0.9=rq(grfm,tau=0.9,data=grilic,model=TRUE) #0.75分位数回归
- summary(grrq0.9);summary(grrq0.9, se = "boot")
- anova(grrq0.1,grrq0.5) #检验不同分位数回归估计值是否相同,零假设系数相同
- anova(grrq0.1,grrq0.5,grrq0.9) #比较不同分位数系数是否相同
- taus <- c(.05,.1,.25,.75,.9,.95)
- grrq=rq(grfm,tau=taus,data=grilic,model=TRUE) #同时回归多个分位数回归
- summary(grrq,se="boot", seed = 500000)
- plot(summary(grrq,se="boot", seed = 520))
- library(uqr)
- data(trust);View(trust)
- formula=Trust_in_the_ECB~Trust_in_the_EU+Trust_in_National_Government
- cre=~Trust_in_the_EU+Trust_in_National_Government
- rif=urq(formula,data=trust,cre=cre,id="countryname")
- rif$coefficients
复制代码第二部分数据为《陈强高计stata》中对应章节数据数据
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|
|
-
总评分: 学术水平 + 5
热心指数 + 5
信用等级 + 5
查看全部评分
|