该【用R语言做非参数 】是由【guoxiachuanyue003】上传分享,文档一共【19】页,该文档可以免费在线阅读,需要了解更多关于【用R语言做非参数 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。用R语言做非参数&半参数回归
笔记
由詹鹏整理,仅供交流和学习根据南京财经大学统计系孙瑞博副教授的课件修改,在此感谢孙老师的辛勤付出教材为:LukeKeele:&Sons,.
第一章introduction:GlobalversusLocalStatistic
一、主要参考书目及说明
1、Hardle(1994).
2、Hardleetc(2004).Nonparametricandsemiparametricmodels:
3、LiandRacine(2007).Nonparametriceconometrics:,偏难
4、PaganandUllah(1999).
5、Yatchew(2003).
6、高铁梅(2009).计量经济分析方法与建模:EVIEWS应用及实例(第二版).清华大学出版社.(P127/143)
7、李雪松(2008)..(P45ch3)
8、陈强(2010)..(ch23/24)【其他参看原ppt第一章】
二、内容简介
方法:
——移动平均(movingaverage)
一一核光滑(Kernelsmoothing)
——K近邻光滑(K-NN)
一局部多项式回归(LocalPolynormal)
一LoesssandLowess
一一样条光滑(SmoothingSpline)
一B-spline
一FriedmanSupersmoother
模型:
一一非参数密度估计一一非参数回归模型一一非参数回归模型一一时间序列的半参数模型
一Paneldata的半参数模型
一QuantileRegression
三、不同的模型形式
1、线性模型linearmodels
F二虑哥硒十牡
2、Nonlinearinvariables
r=A+A^i+A^2
3、Nonlinearinparameters
f=A+A^a
四、数据转换Powertransformation(对参数方法)
IntheGLMframework,modelsareequallyprone(倾向于)tosomemisspecification(不规范)(谨慎的),analystsinthesocialscieneeusuallyrelyonPowerTransformationstoaddressnonlinearity.
[add:(ThirdEdition).AJohnwiley&Sons,Inc.,Publication.(本科的应用回归分析课教材)]
第二章NonparametricDensityEstimation
非参数密度估计
一、三种方法
1、直方图Hiatogram
2、Kerneldensityestimate
3、Knearest-neighborsestimate
二、Histogram对直方图的一个数值解释
Supposex1,...xN-f(x),thedensityfunctionf(x)isunknown.
j(xj二=Lim=Hni
必jj2A
Onecanusethefollowingfunctiontoestimatef(x)
UN~Nh
2h
【与x的距离小于h的所有点的个数】
三、Kerneldensityestimate
T”
Bandwidth:h;windowwidth:2h.
1、Kernelfunction的条件
ThekernelfunctionK(.)isacontinuousfunction,symmetric(对称的)aroundzero,thatintegrates(积分)tounityandsatisfiesadditionalboundedconditions:
⑴K()issymmetricaround0andiscontinuous;
⑵卜⑵总=1j比⑵也=Q何眩s;
Either
(a)K(z)=0if|z|>=zOforzO
|z|K(z)aOas;
—笔,where瞪isaconstant.
(b)
Or
K-emd
KernelFitnctionKiz1
6
UhigMfmboxorrecongular)
|x1(|<1)
l”3M)
Triangularirkuitije)
(1-i(|^|<1)
—
EpSpechmikov(orqu^draticl
^(1a汨h1(UI<1)
L71SK
Quardc(orbiweight)
H(!-瘵xh|:|<1)
Triwciylii
數严谢Xl{\z\<ij
23122
韻1-娜pXL胸<1)
—
G^i^sjan(crnon^nl)
(2^rlyICNp(-;2/2)
.Q7764
Fourth-orKlei(}aussian
—
Founh-tJiikrquanii.'
餐X-FOH+7凸xl(kl<1>
—
(4)
2、主要函数形式
3、置信区间
其中,
町?1为]«+扣计(创為窗画
耐(功"[治卜加=抨/*(月卩耳)业心[亢0卜岂尹CO扭国认+o(春]
4、窗宽的选择
心瑚化护耐“护二5=实际应用中,宀HZTg期心舸)。其中,s是样本标准差,iqr
是样本分位数级差(interquartilerange)
四、Knearest-neighborsestimate
—INt^a
五、R语言部分
da<-("",header=TRUE)lhwage<-da$lhwage
#***bandwidth相等,核函数不同***
deni<-density(lhwage,bw=,kernel="epan")den2<-density(lhwage,bw=,kernel="gauss")den3<-density(lhwage,bw=,kernel="biwe")den4<-density(lhwage,bw=,kernel="rect")
plot(den4,lty=4,main="",xlab="LogHourlywage",ylab="Kerneldensityestimates")lines(den3,lty=3,col="red")lines(den2,lty=2,col="green")lines(deni,lty=1,col="blue")
#***bandwidth不相等,核函数也不同***
den5<-density(lhwage,bw=,kernel="epan")den6<-density(lhwage,bw=,kernel="gauss")
den7<-density(lhwage,bw=,kernel="biwe")den8<-density(lhwage,bw=,kernel="rect")
plot(den8,lty=4,main="",xlab="LogHourlywage",ylab="Kerneldensityestimates")lines(den7,lty=3,col="red")lines(den6,lty=2,col="green")lines(den5,lty=1,col="blue")
第三章smoothingandlocalregression
一、简单光滑估计法SimpleSmoothing
1、LocalAveraging局部均值
按照x排序,将样本分成若干部分(intervalsor“bins”);将每部分x对应的y值的均值作为f(x)的估计。
三种不同方法:
相同的宽度(equalwidthbins):uniformlydistributed.
相同的观察值个数():k-nearestneighbor.
移动平均(movingaverage)
K—NN:
-1
二■■■…乃+t坯)=
j-L
2、kernelsmoothing核光滑
亍①二丈弔(弧
J-1
叫®=■h=
其中,
i-l
二、局部多项式估计LocalPolynomialRegression
1、主要结构
局部多项式估计是核光滑的扩展,也是基于局部加权均值构造。
——localconstantregression
一locallinearregression
一lowess(Cleveland,1979)
一loess(Cleveland,1988)
【本部分可参考:
Takezana(2006).IntroductiontoNonparametric
Regression.()
ChambersandHastie(1993).Statisticalmodelsins.(P312ch8)】
2、方法思路
对于每个xi,以该点为中心,按照预定宽度构造一个区间;
在每个结点区域内,采用加权最小二乘法(WLS)估计其参数,并用得到的模型估计该结点对应的x值对应y值,作为y|xi的估计值(只要这一个点的估计值);
估计下一个点xj;
将每个y|xi的估计值连接起来。
【R操作
library(KernSmooth)#函数locpolyO
library(locpol)#locpol();locCteSmootherC()library(locfit)#locfit()
#weightfunciton:kernel=”tcub”.And“rect”,“trwt”,“tria”,“epan”,“bisq”,“gauss”
】
3、每个方法对应的估计形式
变量个数p=0,localconstantregression(kernelsmoothing)
min\nJz
变量个数p=1,locallinearregression
P二A+冷+疋
H叶炖-W
minjjl矗丿
Lowess(LocalWeightedscatterplotsmoothing)
p=1:¥二执十跑*
=i^\宁br-w
minul起丿
【还有个加权修正的过程,这里略,详见原书或者PPT】
Loess(Localregression)
p=1,2:
碍尽=i^\辛b厂棉-肛-角』
min
【还有个加权修正的过程,这里略,详见原书或者PPT】
Friedmansupersmoother
symmetrick-NN,usinglocallinearfit,varyingspan,whichisdeterminedbylocalCV,notrobusttooutliers,fasttocomputesupsmu()inR
三、模型选择
需要选择的内容:(1)窗宽thespan;(2)多项式的度thedegreeofpolynomialforthelocalregressionmodels(3)权重函数theweightfunctions。
【其他略】
四、R语言部分
library(foreign)library(SemiPar)library(mgcv)jacob<-("",header=TRUE)###############################################################################
#第一部分,简单的光滑估计
#1、KernelDensityEstimation#IllustrationofKernelConcepts#DefiningthewindowWidthattach(jacob)
x0<-sort(perotvote)[75]diffs<-abs(perotvote-x0)<-sort(diffs)[120]#ApplyingtheTricubeWeight#...Tricubefunctiontricube<-function(z){ifelse(abs(z)<1,(1-(abs(z))人3)人3,0)}
#...
a<-seq(0,1,by=.1)
tricube(a)
#
plot(range(perotvote),c(0,1),xlab="PerotVote(%)",ylab="Tricubeweight",type='n',bty="l")
abline(v=c(x0-,xO+),lty=2)abline(v=xO)
xwts<-seq(xO-,xO+,len=250)
lines(xwts,tricube((xwts-xO)/),lty=1,lwd=1)points(,tricube((-x0)/),cex=1)###########################################################################
#2、KernelSmoothing###########################################################################
par(mfrow=c(3,1))
plot(perotvote,,pch=".",cex=,xlab="PerotVote(%)",ylab="Challenger'sVoteShare(%)",main="Bandwidth=4",bty="l")
lines(ksmooth(perotvote,,bandwidth="4"))plot(perotvote,,pch=".",cex=.65,xlab="PerotVote(%)",ylab="Challenger'sVoteShare(%)",main="Bandwidth=8",bty="l")
lines(ksmooth(perotvote,,
kernel="box",bandwidth="8"),lty=1)plot(perotvote,,pch=".",cex=.65,xlab="PerotVote(%)",ylab="Challenger'sVoteShare(%)",main="Bandwidth=12",bty="l")
lines(ksmooth(perotvote,,bandwidth="12"),lty=1)********Kernelsmoothing中选取box和normal核函数的比较,带宽相等
plot(perotvote,,pch=".",cex=.65,xlab="PerotVote(%)",ylab="Challenger'sVoteShare(%)",
main="Bandwidth=8",bty="l")
lines(ksmooth(perotvote,,_
kernel="box",bandwidth="8"),lty=1)
lines(ksmooth(perotvote,,
kernel="normal",bandwidth="8"),lty=2,col="red")
##################################################################################
#第二部分,LPR模型
#DataPrepForLocalAverageRegressionStep-by-Step
cong<-(jacob[,2:3])
cong<-cong[order(cong$perotvote),1:2]
y<-(cong$)
x<-(cong$perotvote)
n<-length(y)
#...
tricube<-function(z){
ifelse(abs(z)<1,(1-(abs(z))人3)人3,0)
}
#...
xO<-x[75]
diffs<-abs(x-xO)
<-sort(diffs)[120]
n<-x[diffs<=]
<-y[diffs<=]
weigh=tricube((-x0)/)
mod<-lm(~,weights=weigh)
#
plot(x,y,type="n",cex=.65,xlab="PerotVote(%)",ylab="Challenger'sVoteShare(%)",bty="l")
abline(v=c(xO-,x0+),lty=2)abline(v=x0)
points(x[diffs>],y[diffs>],pch=16,cex=1,col=gray(.80))
points(x[diffs<=],y[diffs<=],cex=.85)
abline(mod,lwd=2,col=1)
text(,50,expression(paste("FittedValueofyat",x[0])))#这里expression的用法比较有意思
arrows(25,47,15,37,code=2,length=.10)#################################################################################
#2、NowPuttingItTogetherForLocalRegression
Demonstration.
#OLSFitforComparison
ols<-lm(~perotvote,data=jacob)
#Theloessfit
<-loess(~perotvote,data=jacob,span=)
#***默认设置degree=2,family=gauss,tricube力口权***
n<-length()
loess<-seq(min(perotvote),max(perotvote),length=n)
<-predict(,(perotvote=))#得至U预测值便于比较
#Thelowessfit
<-lowess(~perotvote,data=jacob,f=)
#***默认设置robustlineartricube加权***
n<-length()
lowess<-seq(min(perotvote),max(perotvote),length=n)
<-predict(,
(perotvote=))#得至U预测值便于比较
#
plot(perotvote,,pch=".",
ylab="Challengers'VoteShare(%)",xlab="voteforPerot
(%)",bty="l")
lines(,)
lines(,)
abline(ols)
legend(15,2O,c("Loess","Lowess","OLS"),lty=c(1,2,1),bty="n",cex=.8)
#################################################################################
#3、lowess中不同robust的比较
<-lowess(perotvote,,f=,iter=0)
#***没有进行第二步的robust加权估计***
<-lowess(perotvote,,f=)
#***默认iter=3,要进行3次robust加权估计***
<-loess(~perotvote,data=jacob,span=,degree=1,family="symm",iterations")#**norobust
<-loess(~perotvote,data=jacob,span=,degree=1)#***没有进行第二步的robust加权估计***<-loess(~perotvote,data=jacob,span=,degree=1,family="symm",iterations=3)
#***进行3次robust加权估计***
plot(perotvote,,pch=".",ylab="Challengers'Vote
Share(%)",xlab="voteforPerot(%)")
lines()
lines(sort(perotvote),$fit[order(perotvote)],lty=3,col="green")
lines(sort(perotvote),$fit[order(perotvote)],lty=9,col=18)
lines(,lty=2,col="red")
lines(sort(perotvote),$fit[order(perotvote)],lty=4,col="blue")
第四章样条估计spline
一、基本思想
按照x将样本分成多个区间,对每个区间分别进行估计。不同于核估计,这里不用移动计算,从而减小了计算量。
二、最简单的形式
LinearSplinewithkknots:
jr二吗十吗工卄石仗一叼)★十T眞仗一q)+*壬
=%+°^(功+CO+tAAHR+£
{
x->C
1J
八」0工仏,%(Q二&-也
三、其他样条模型
1、p次样条估计
一一二次样条QuadraticSpline(basisfunctionswithkknots)
y=^o+好*切+A(^-叼)7+—+A(x_ct£+f
一一三次样条CubicSpline(withkknots,usequadraticbasisfunctions)
P=吗十%"心哥咛3十A仗一辑+—1■屛(工一cj:址
一p-orderspline(withkknots)
y=^o+a^+-+咛』+At^-d+—+AU-^+£
2、B-splines(withkknotscubicB-splinebasis)
/w=叫十吟■另鹏CO
其中,
1G-<X<C-j
Lood^+北心
电p_ci务prt_Ch1
3、NaturalSplines
以上估计方法对结点(knots)之间的估计比较准确,但对边界的拟合效果较差。自然样条的思想是,在自变量最小值和最大值处各增加一个结点,用线性模型拟合边界位置的样本点。
用R语言做非参数 来自淘豆网m.daumloan.com转载请标明出处.