全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
2011-4-4 00:09:51
epoh前辈,您说过rq和rqss根本不能实现多个分位数同时估计的,而且使用rq函数,内部就有画图程序,效果和您的是一样的。如plot(summary(...))就可以。
我的意思是在rq.fit.sfn函数基础上(即使用rq.fit.panel函数),使用bootstrap出来的标准误直接计算出置信区间,进而画出图形



n1=length(y)  #30
b=1000     #Number of bootstrap samples
ncoef=16
boot_bhat=matrix(NA,b, ncoef)
block_length = 10  
num_blocks = n1/block_length    #n/block_length  
Indices = seq(1:n1)  # All of the indices from 1 to n
Indices = matrix(Indices,block_length,num_blocks)
for (i in 1:b){          #Number of bootstrap samples
randblock =sample(seq(1:num_blocks),num_blocks,replace = TRUE) # Choose which blocks to use
Ind_sim = Indices[,randblock]       #Find which data are in each block  
Ind_sim = c(Ind_sim)      
Xsim = X[Ind_sim,1:dim(X)[2]]      #Construct the x data
Ysim = y[Ind_sim]          #Construct the y data
boot_bhat[i,] = rq.fit.panel(Xsim,Ysim,s)$coef
}
bhat=colMeans(boot_bhat)
#bhat

cov=cov(boot_bhat)
serr = sqrt(diag(cov))
#serr   

p <- length(bhat)
rdf <- n1 - p
vnames<- dimnames(x)[[2]]
coef <- array(bhat, c(p, 4))
dimnames(coef) <- list(vnames, c("Value", "Std. Error", "t value","Pr(>|t|)"))
coef[, 2] <- serr
coef[, 3] <- coef[, 1]/coef[, 2]
coef[, 4] <- if (rdf > 0) 2 * (1 - pt(abs(coef[, 3]), rdf))
coef
就是在上面计算出系数coef后,直接计算置信区间,进而再画出图形(尽管现在估计的分位点较少,但是我只是想看大致趋势,没有关系)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 09:59:57
在function rq.fit.sfn()基础上
底下结果都相同,你要的是哪种图形
fit1=rq.fit.sfn(xx,yy,tau=0.5,a)
fit1
fit2=rq.fit.sfn(xx,yy,tau=0.3,a)
fit2
fit3=rq.fit.sfn(xx,yy,tau=0.7,a)
fit3
fit4=rq.fit.sfn(xx,yy,tau=c(0.1,0.25,0.5,0.75,0.9) ,a)
fit4


另针你说用bootstrap结果不稳定,
应该说bootstrap结果是稳定,
是我block bootstrap程序,忙中出错.

还是回到我提供的BOOTSTRAP FOR PANEL DATA MODELS.pdf
page 6/30 block bootstrap resampling is operating with columns
所以根据page 2/30
panel dataset with N individuals and T time periods is represented
              by a matrix Y of N rows and T columns

就以提供的数据为例:
year=c(1991,1992,1993,1994,1995,1996,1991,1992,1993,1994,1995,1996)
x1=c(10,20,30,40,50,60,70,80,90,100,110,120)
x2=c(101,201,301,401,501,601,701,801,901,1001,1101,1201)
x=cbind(x1,x2)
y=year
#x
> x
      year  x1   x2
[1,] 1991  10  101
[2,] 1992  20  201
[3,] 1993  30  301
[4,] 1994  40  401
[5,] 1995  50  501
[6,] 1996  60  601
[7,] 1991  70  701
[8,] 1992  80  801
[9,] 1993  90  901
[10,] 1994 100 1001
[11,] 1995 110 1101
[12,] 1996 120 1201

数据需先转换成下列pattern, 再以 column 抽样

   T 1991 1992 1993 1994 1995 1996
              10      20     30      40      50      60
      id  701    801   901 1001  1101 1201

然后再还原成paneldata格式,进行回归.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 10:50:21
epoh前辈,呵呵,我还是没有表达清楚,勿怪
我要的图形应该是fit4=rq.fit.sfn(xx,yy,tau=c(0.1,0.25,0.5,0.75,0.9) ,a)
意思就是说假如现在我有4个解释变量(包括intercept),根据上面命令,每个解释变量都估计了5个分位点
我想直观观察每个解释变量随着分位点不同,其值的变化规律。
就是以分位点(0.1,0.25,0.5,0.75,0.9)为横轴,而4个解释变量的系数值为纵轴,这样就画出了4幅反映
各解释变量规律的图像了。如
n1=length(y)  #30
b=1000     #Number of bootstrap samples
ncoef=16
boot_bhat=matrix(NA,b, ncoef)
block_length = 10  
num_blocks = n1/block_length    #n/block_length  
Indices = seq(1:n1)  # All of the indices from 1 to n
Indices = matrix(Indices,block_length,num_blocks)
for (i in 1:b){          #Number of bootstrap samples
randblock =sample(seq(1:num_blocks),num_blocks,replace = TRUE) # Choose which blocks to use
Ind_sim = Indices[,randblock]       #Find which data are in each block  
Ind_sim = c(Ind_sim)      
Xsim = X[Ind_sim,1:dim(X)[2]]      #Construct the x data
Ysim = y[Ind_sim]          #Construct the y data
boot_bhat[i,] = rq.fit.panel(Xsim,Ysim,s)$coef
}
bhat=colMeans(boot_bhat)
#bhat

cov=cov(boot_bhat)
serr = sqrt(diag(cov))
#serr   

p <- length(bhat)
rdf <- n1 - p
vnames<- dimnames(x)[[2]]
coef <- array(bhat, c(p, 4))
dimnames(coef) <- list(vnames, c("Value", "Std. Error", "t value","Pr(>|t|)"))
coef[, 2] <- serr
coef[, 3] <- coef[, 1]/coef[, 2]
coef[, 4] <- if (rdf > 0) 2 * (1 - pt(abs(coef[, 3]), rdf))
coef
如上面您已经bootstrap得出了各解释变量在不同分位点的系数,那就想把这些系数趋势画在图形中反映出来


我前面已经说了,呵呵,不是bootstrap不稳定问题,而是我自己参数设置问题
啊,bootstrap抽样有错误吗?那前辈能修改吗?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 13:48:34
library(SparseM)
library(quantreg)
library(Ecdat)
data(Grunfeld)
#panel data Grunfeld
#n=10, T=20, N=200
x1=Grunfeld$value
x2=Grunfeld$capital
X=cbind(x1,x2)
y=Grunfeld$inv
s= rep(1:10,rep(20,10))
lambda=1
w=c(0.1,0.2,0.3,0.4,0.5,0.4,0.3,0.2,0.1)
taus=c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)
ntaus=length(taus)
K <- length(w)
X <- as.matrix(X)
p <- ncol(X)
n <- length(levels(as.factor(s)))
N <- length(y)
Z <- as.matrix.csr(model.matrix(~as.factor(s)-1))
Fidelity <- cbind(as(w,"matrix.diag.csr") %x% X,w %x% Z)  
Penalty <- cbind(as.matrix.csr(0,n,K*p),lambda*as(n,"matrix.diag.csr"))  
D <- rbind(Fidelity,Penalty)   
yy <- c(w %x% y,rep(0,n))
a <- c((w*(1-taus)) %x% (t(X)%*%rep(1,N)),
       sum(w*(1-taus)) * (t(Z) %*% rep(1,N)) + lambda * rep(1,n))  
rq.fit.sfn(D,yy,tau=taus,rhs=a)
#####block bootstrap
#n=10, T=20, N=200
N=length(y)  #200
T=20
n=10
blockmat=matrix(NA,n,T)
for(i in 1:n){
   for(j in 1:T){
  #    blockmat[i,j]=(j-1)*T+i
        blockmat[i,j]=(i-1)*T+j
      }
}
b=5000   #Number of bootstrap samples
ncoef=28
boot_bhat=matrix(NA,b, ncoef)
indices = seq(1:T)  # All of the indices from 1 to 20
####

for (j in 1:b){          #Number of bootstrap samples
randblock =sample(indices,replace = TRUE) # Choose which blocks to use
ind_sim=0*seq(1:n)
   for(i in 1:n){
    ind_sim[((i-1)*T+1):(i*T)]=(i-1)*T+randblock
          }   
xsim=cbind(x1[ind_sim],x2[ind_sim]) #Construct the X data
xx1=cbind(as(w,"matrix.diag.csr") %x% xsim,w %x% Z)
xx<-rbind(xx1,Penalty)
ysim = y[ind_sim]          #Construct the y data
yy <- c(w %x% ysim,rep(0,n))
a <- c((w*(1-taus)) %x% (t(xsim)%*%rep(1,N)),
                sum(w*(1-taus)) * (t(Z) %*% rep(1,N)) + lambda * rep(1/2,n))
boot_bhat[j,] = rq.fit.sfn(xx,yy,taus,a)$coef
}

bhat=colMeans(boot_bhat)
#bhat
cov=cov(boot_bhat)
serr = sqrt(diag(cov))
#serr   
p <- length(bhat)
rdf <- N-ncoef
coef <- array(bhat, c(p, 4))
dimnames(coef) <- list(seq(1:ncoef), c("Value", "Std. Error", "t value","Pr(>|t|)"))
coef[, 2] <- serr
coef[, 3] <- coef[, 1]/coef[, 2]
coef[, 4] <- if (rdf > 0) 2 * (1 - pt(abs(coef[, 3]), rdf))
coef
> coef
           Value  Std. Error    t value     Pr(>|t|)
1     0.06107249  0.01386247  4.4055984 1.850521e-05
2     0.15487193  0.02205991  7.0205165 4.903056e-11
3     0.07234995  0.01482287  4.8809663 2.396156e-06
4     0.16567351  0.02101250  7.8845210 3.477219e-13
5     0.08053246  0.01565537  5.1440794 7.267875e-07
6     0.17087047  0.02093900  8.1603941 6.794565e-14
7     0.08633521  0.01577335  5.4734852 1.539569e-07
8     0.17589873  0.02234190  7.8730426 3.721468e-13
9     0.08989036  0.01543300  5.8245559 2.752130e-08
10    0.18514027  0.02556903  7.2408017 1.425859e-11
11    0.09275225  0.01509710  6.1437128 5.434659e-09
12    0.19914111  0.03041511  6.5474402 6.494829e-10
13    0.09563246  0.01524897  6.2714042 2.799298e-09
14    0.21819694  0.03619383  6.0285673 9.816594e-09
15    0.09971964  0.01643330  6.0681427 8.017352e-09
16    0.24902179  0.05148496  4.8367874 2.915302e-06
17    0.10538962  0.02081095  5.0641423 1.048965e-06
18    0.31456336  0.07023116  4.4789711 1.362888e-05
19   70.87586275 53.31286209  1.3294327 1.854658e-01
20  178.11811868 39.54191280  4.5045398 1.224064e-05
21 -145.88272883 33.85621760 -4.3088903 2.754051e-05
22   -1.70397795  8.63275211 -0.1973853 8.437591e-01
23  -51.72378988 17.42847433 -2.9677750 3.427315e-03
24   -1.91490600  6.15461311 -0.3111334 7.560759e-01
25  -24.71403762  8.68786452 -2.8446619 4.985616e-03
26  -31.82235648 11.51383878 -2.7638355 6.335172e-03
27  -42.08427967 10.15349925 -4.1448055 5.328559e-05
28   -4.48096796  0.96710385 -4.6333886 7.076866e-06
###############
coef1mat=matrix(NA,ntaus,5)
coef2mat=matrix(NA,ntaus,5)
#If n is large(>1000), t=1.96
t=1.96
for (i in 1:ntaus){
#coefficient1
coef1mat[i,1]=taus
coef1mat[i,2]=coef[(2*(i-1)+1),1]
coef1mat[i,3]=coef[(2*(i-1)+1),2]
coef1mat[i,4]=coef[(2*(i-1)+1),1]+t*coef[(2*(i-1)+1),2]
coef1mat[i,5]=coef[(2*(i-1)+1),1]-t*coef[(2*(i-1)+1),2]
#coefficient2
coef2mat[i,1]=taus
coef2mat[i,2]=coef[2*i,1]
coef2mat[i,3]=coef[2*i,2]
coef2mat[i,4]=coef[2*i,1]+t*coef[2*i,2]
coef2mat[i,5]=coef[2*i,1]-t*coef[2*i,2]
}
coef1mat
coef2mat
> coef1mat
      [,1]       [,2]       [,3]       [,4]       [,5]
[1,]  0.1 0.06107249 0.01386247 0.08824294 0.03390204
[2,]  0.2 0.07234995 0.01482287 0.10140278 0.04329712
[3,]  0.3 0.08053246 0.01565537 0.11121698 0.04984793
[4,]  0.4 0.08633521 0.01577335 0.11725098 0.05541944
[5,]  0.5 0.08989036 0.01543300 0.12013903 0.05964168
[6,]  0.6 0.09275225 0.01509710 0.12234257 0.06316193
[7,]  0.7 0.09563246 0.01524897 0.12552045 0.06574448
[8,]  0.8 0.09971964 0.01643330 0.13192891 0.06751036
[9,]  0.9 0.10538962 0.02081095 0.14617909 0.06460016
> coef2mat
      [,1]      [,2]       [,3]      [,4]      [,5]
[1,]  0.1 0.1548719 0.02205991 0.1981093 0.1116345
[2,]  0.2 0.1656735 0.02101250 0.2068580 0.1244890
[3,]  0.3 0.1708705 0.02093900 0.2119109 0.1298300
[4,]  0.4 0.1758987 0.02234190 0.2196889 0.1321086
[5,]  0.5 0.1851403 0.02556903 0.2352556 0.1350250
[6,]  0.6 0.1991411 0.03041511 0.2587547 0.1395275
[7,]  0.7 0.2181969 0.03619383 0.2891369 0.1472570
[8,]  0.8 0.2490218 0.05148496 0.3499323 0.1481113
[9,]  0.9 0.3145634 0.07023116 0.4522164 0.1769103
###plot coef1,coef2 95%CI
par(mfrow = c(1, 2))
####coefficient1
upperbd1=coef1mat[,4]
lowerbd1=coef1mat[,5]
coef1=coef1mat[,2]
plot(taus,upperbd1,type = 'n',  ylim = c(0.00, 0.20),
     ylab = 'Coef1 Value', xlab = 'Quantile')
lines(taus, lowerbd1, col = 'grey')
lines(taus, upperbd1, col = 'grey')
polygon(c(taus, rev(taus)), c(upperbd1, rev(lowerbd1)),
     col = "lightblue", border = 'black')
points(taus, coef1,col='blue',pch=21)
lines(taus, coef1,col='blue')
title("gross Investment vs value of the firm")
####coefficient2
upperbd2=coef2mat[,4]
lowerbd2=coef2mat[,5]
coef2=coef2mat[,2]
plot(taus,upperbd2,type = 'n',  ylim = c(0.00, 0.50),
     ylab = 'Coef2 Capital', xlab = 'Quantile')
lines(taus, lowerbd2, col = 'grey')
lines(taus, upperbd2, col = 'grey')
polygon(c(taus, rev(taus)), c(upperbd2, rev(lowerbd2)),
     col = "lightblue", border = 'black')
points(taus, coef2,col='blue',pch=21)
lines(taus, coef2,col='blue')
title("gross Investment vs stock of plant and equipment")
          graph.jpeg
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 17:25:52
epoh前辈,谢谢您
可以看出,您很认真负责
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 23:08:46
epoh前辈,我运行这一段时,会提示错误
t=1.96
for (i in 1:ntaus){
#coefficient1
coef1mat[i,1]=taus
coef1mat[i,2]=coef[(2*(i-1)+1),1]
coef1mat[i,3]=coef[(2*(i-1)+1),2]
coef1mat[i,4]=coef[(2*(i-1)+1),1]+t*coef[(2*(i-1)+1),2]
coef1mat[i,5]=coef[(2*(i-1)+1),1]-t*coef[(2*(i-1)+1),2]
#coefficient2
coef2mat[i,1]=taus
coef2mat[i,2]=coef[2*i,1]
coef2mat[i,3]=coef[2*i,2]
coef2mat[i,4]=coef[2*i,1]+t*coef[2*i,2]
coef2mat[i,5]=coef[2*i,1]-t*coef[2*i,2]
}
coef1mat
coef2mat

错误于coef1mat[i, 1] = taus : 被替换的项目不是替换值长度的倍数

我哪里出错?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 23:15:08
coef1mat[i,1]=taus[i]
coef2mat[i,1]=taus[i]
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-4 23:48:35
额,epoh老师,已经可以了
谢谢您的指导
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-8 18:32:28
epoh老师,又碰到疑问了
我看到一些文章运行出各分位点的系数后,如在0.1,0.2,0.3,0.4,0.5的系数值分别为coeff1,coeff2,coeff3,coeff4,coeff5,文章提到了通过统计量检验这些系数间是否存在显著差异。有些文章提到了系数的联合显著性检验(ie,有些文章提到:对FDI在不同分位点的各系数差异性的联合F检验),这些在R有相关命令吗?
附件中有一篇文章,其中我用红色标志了2个地方,您看在R中有相关命令可以实现吗?
附件列表
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-9 08:56:00
function anova.rq()

There are two (as yet) distinct forms of the test.
In the first the fitted objects all have the same specified quantile (tau) and
the intent is to test the hypothesis that smaller models are adequate
relative to the largest specified model.

In the second form of the test the linear predictor of the fits
are all the same, but the specified quantiles (taus) are different
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-9 10:41:29
epoh前辈,anova.rq函数是和rq函数一起使用的
ie:
data(barro)
fit0 <- rq(y.net ~  lgdp2 + fse2 + gedy2 , data = barro)
fit1 <- rq(y.net ~  lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro)
fit2 <- rq(y.net ~  lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.75)
fit3 <- rq(y.net ~  lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.25)
anova(fit1,fit0)
anova(fit1,fit2,fit3)
anova(fit1,fit2,fit3,joint=FALSE)

我要的是anova(fit1,fit2,fit3,joint=FALSE)这个实现结果。我看了anova.rq内部函数,呵呵,好复杂啊
内部程序:
> anova.rq
function (object, ...)
{
    if (length(list(object, ...)) > 1) {
        return(anova.rqlist(object, ...))
    }
    stop("Anova is only defined (yet) for lists of rq objects")
}
> anova.rqlist
function (object, ..., test = "Wald", joint = TRUE, score = "wilcoxon",
    R = 200, trim = NULL)
{
    objects <- list(object, ...)
    responses <- as.character(lapply(objects, function(x) formula(x)[[2]]))
    sameresp <- responses == responses[1]
    if (!all(sameresp))
        stop("Models don't all have the same response variable")
    n <- length(objects[[1]]$y)
    models <- as.character(lapply(objects, function(x) formula(x)))
    nobjects <- length(objects)
    dimp <- lapply(objects, function(x) length(coef(x)))
    objects <- objects[order(-unlist(dimp))]
    mf <- model.frame(objects[[1]])
    models <- as.character(lapply(objects, function(x) formula(x)))
    taus <- unlist(lapply(objects, function(x) x$tau))
    if (is.matrix(coef(objects[[1]])))
        names <- lapply(objects, function(x) dimnames(coef(x))[[1]])
    else names <- lapply(objects, function(x) names(coef(x)))
    if (test == "Wald")
        objects <- lapply(objects, function(x) summary(x, se = "nid",
            covariance = TRUE))
    sametaus <- taus == taus[[1]]
    if (all(sametaus)) {
        Tn <- rep(0, nobjects - 1)
        ndf <- Tn
        ddf <- Tn
        pvalue <- Tn
        topnote <- paste("Model ", format(1:nobjects), ": ",
            models, sep = "", collapse = "\n")
        if (test == "anowar") {
            X1 <- model.matrix(objects[[1]], mf, contrasts = objects[[1]]$contrasts)
            y <- model.response(mf)
            weights <- model.weights(mf)
            tau <- taus[[1]]
            for (i in 2:nobjects) {
                if (!all(names[[i]] %in% names[[1]]))
                  stop("Models aren't nested")
                mf <- model.frame(objects[[i]])
                X0 <- model.matrix(objects[[i]], mf, contrasts = objects[[i]]$contrasts)
                Htest <- rq.test.anowar(X0, X1, y, tau = tau,
                  R = R)
                ndf[i - 1] <- Htest$ndf
                Tn[i - 1] <- Htest$Tn
                ddf[i - 1] <- Htest$ddf
                pvalue[i - 1] <- Htest$pvalue
            }
            table <- data.frame(ndf, ddf, Tn, pvalue)
        }
        else if (test == "rank") {
            x1 <- model.matrix(objects[[1]], mf, contrasts = objects[[1]]$contrasts)
            y <- model.response(mf)
            weights <- model.weights(mf)
            for (i in 2:nobjects) {
                if (!all(names[[i]] %in% names[[1]]))
                  stop("Models aren't nested")
                nullH <- is.na(match(names[[1]], names[[i]]))
                X1 <- as.matrix(x1[, nullH])
                mf <- model.frame(objects[[i]])
                X0 <- model.matrix(objects[[i]], mf, contrasts = objects[[i]]$contrasts)
                if (score == "tau")
                  tau <- taus[[1]]
                Htest <- rq.test.rank(X0, X1, y, score = score,
                  weights = weights, tau = tau, trim = trim)
                ndf[i - 1] <- Htest$ndf
                Tn[i - 1] <- Htest$Tn
                ddf[i - 1] <- Htest$ddf
                pvalue[i - 1] <- Htest$pvalue
            }
            table <- data.frame(ndf, ddf, Tn, pvalue)
        }
        else if (test == "Wald") {
            V <- lapply(objects, function(x) x$cov)
            coef <- lapply(objects, function(x) coef(x)[, 1])
            for (i in 2:nobjects) {
                if (!all(names[[i]] %in% names[[1]]))
                  stop("Models aren't nested")
                nullH <- is.na(match(names[[1]], names[[i]]))
                ndf[i - 1] <- sum(nullH)
                Tn[i - 1] <- t((coef[[1]])[nullH]) %*% solve((V[[1]])[nullH,
                  nullH], (coef[[1]])[nullH])/ndf[i - 1]
                ddf[i - 1] <- n - length(names[[1]])
                pvalue[i - 1] <- 1 - pf(Tn[i - 1], ndf[i - 1],
                  ddf[i - 1])
            }
            table <- data.frame(ndf, ddf, Tn, pvalue)
        }
        else stop("test only defined for anowar, Wald and rank")
    }
    else {
        m <- length(taus)
        for (i in 2:m) {
            if (!setequal(names[[i]], names[[1]]))
                stop("Models with common tau don't have same X")
        }
        if (names[[1]][1] != "(Intercept)")
            stop("Intercept required in common tau testing")
        Omega <- outer(taus, taus, pmin) - outer(taus, taus)
        J <- objects[[1]]$J
        p <- dim(J)[1]
        H <- array(unlist(lapply(objects, function(x) x$Hinv)),
            c(p, p, m))
        H <- matrix(aperm(H, c(1, 3, 2)), p * m, p) %*% t(chol(J))
        W <- (H %*% t(H)) * (kronecker(Omega, outer(rep(1, p),
            rep(1, p))))
        coef <- unlist(lapply(objects, function(x) coef(x)[,
            1]))
        if (joint) {
            D <- kronecker(diff(diag(m)), cbind(0, diag(p - 1)))
            ndf <- (p - 1) * (m - 1)
            Tn <- t(D %*% coef) %*% solve(D %*% W %*% t(D), D %*%
                coef)/ndf
            ddf <- n * m - (p - 1) * (m - 1)
            pvalue <- 1 - pf(Tn, ndf, ddf)
            nobjects <- 1
            tnote1 <- paste("Model: ", models[[1]], "\n", sep = "")
            tnote2 <- paste("Joint Test of Equality of Slopes: tau in { ",
                paste(taus, collapse = " "), " }\n")
            topnote <- paste(tnote1, tnote2, sep = "")
            table <- data.frame(ndf, ddf, Tn, pvalue)
        }
        else {
            Tn <- pvalue <- rep(0, p - 1)
            ndf <- m - 1
            ddf <- n * m - (m - 1)
            for (i in 2:p) {
                E <- matrix(0, 1, p)
                E[1, i] <- 1
                D <- kronecker(diff(diag(m)), E)
                Tn[i - 1] <- t(D %*% coef) %*% solve(D %*% W %*%
                  t(D), D %*% coef)/ndf
                pvalue[i - 1] <- 1 - pf(Tn[i - 1], ndf, ddf)
            }
            tnote1 <- paste("Model: ", models[[1]], "\n", sep = "")
            tnote2 <- paste("Tests of Equality of Distinct Slopes: tau in { ",
                paste(taus, collapse = " "), " }\n")
            topnote <- paste(tnote1, tnote2, sep = "")
            table <- data.frame(ndf, ddf, Tn, pvalue)
            dimnames(table)[[1]] <- names[[1]][2:p]
        }
    }
    x <- list(table = table, topnote = topnote)
    class(x) <- "anova.rq"
    return(x)
}

呵呵,看来rq.sfn.fit这个函数还是存在很大缺陷,主要还是用rq()函数
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-9 11:01:21
哈哈!老兄,
应该说package 更新的速度,
不及你研究的"深度".
我稍瞄一下:
The standard errors (in parenthesis) are obtained after 1000 panel-bootstrap replications
我抽空,看看能否有解.
你有没这组DATA?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2011-4-9 11:19:35
epoh前辈,见笑了,呵呵。我只是对方法很感兴趣。
其实这些估计分位的函数,包括quantreg包都是roger教授编写的。他专门写了一个rq.fit.panel(不属于R内部函数),其实就是调用rq.fit.sfn函数来估计面板数据分位,而据他主页上介绍rqss()函数和rq()函数也是可以实现这一功能了。不过前段时间您已经帮忙看了,他只能一次估计一个分位点,而不能同时估计多个分位点。

这篇文章是roger教授的学生写的一篇,这位老师一般都不共享数据的,所以我也没有数据。
额,标准误是通过panel-bootstrap产生的,这个您已经实现了。

哈哈,可惜epoh老师不是专门学习quantile regression的,要不我就可以向您请教疑问了。
我现在还堆积着很多问题没有得到解答,看的深度不够,基础不扎实啊。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-9 17:43:39
epoh 发表于 2011-4-4 13:48
library(SparseM)
library(quantreg)
library(Ecdat)
前辈,您好,在论坛里看到您是R语言的高手,我问一下,为什么欲行面板分位数回归时总是出问题:
> library(SparseM)
> library(quantreg)
> library(Ecdat)
> data(Grunfeld)
> #panel data Grunfeld
> #n=10, T=20, N=200
> x1=Grunfeld$value
> x2=Grunfeld$capital
> X=cbind(x1,x2)
> y=Grunfeld$inv
> s= rep(1:10,rep(20,10))
> lambda=1
> w=c(0.1,0.2,0.3,0.4,0.5,0.4,0.3,0.2,0.1)
> taus=c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9)
> ntaus=length(taus)
> K <- length(w)
> X <- as.matrix(X)
> p <- ncol(X)
> n <- length(levels(as.factor(s)))
> N <- length(y)
> Z <- as.matrix.csr(model.matrix(~as.factor(s)-1))
> Fidelity <- cbind(as(w,"matrix.diag.csr") %x% X,w %x% Z)  
错误于cbind(deparse.level, ...) : args have differing numbers of rows
此外: 警告信息:
In as.matrix.csr(x, nrow = nrow, ncol = ncol, eps = eps) :
  ncol*nrow indivisable by length(x)
> Penalty <- cbind(as.matrix.csr(0,n,K*p),lambda*as(n,"matrix.diag.csr"))  
> D <- rbind(Fidelity,Penalty)   
错误于rbind(Fidelity, Penalty) : 找不到对象'Fidelity'
> yy <- c(w %x% y,rep(0,n))
> a <- c((w*(1-taus)) %x% (t(X)%*%rep(1,N)),
+        sum(w*(1-taus)) * (t(Z) %*% rep(1,N)) + lambda * rep(1,n))  
> rq.fit.sfn(D,yy,tau=taus,rhs=a)
请您指教
我计算过,一个是200*18的矩阵,一个是200*90的矩阵,为什么会提示说行不同呢??请您指点一下
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-9 22:17:34
sun1018 发表于 2012-4-9 17:43
前辈,您好,在论坛里看到您是R语言的高手,我问一下,为什么欲行面板分位数回归时总是出问题:
> libra ...
嘿嘿,一年了,有点忘了
不知ywh兄,还记不记得?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-9 22:53:50
epoh 发表于 2012-4-9 22:17
嘿嘿,一年了,有点忘了
不知ywh兄,还记不记得?
呵呵,刚才仔细看了一下, 是W的定义有问题
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-10 08:39:12
epoh 发表于 2012-4-9 22:17
嘿嘿,一年了,有点忘了
不知ywh兄,还记不记得?
呵呵,epoh老师,楼主自己解决了。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-10 16:46:27
sun1018 发表于 2012-4-9 22:53
呵呵,刚才仔细看了一下, 是W的定义有问题
DB file 是MicroTSP 的数据文件,在TSP中用fetch得到。
epoh老师,DB结尾的数据文件在TSP中如何打开呢?
好像是一个网页数据。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-10 21:07:16
ywh19860616 发表于 2012-4-10 16:46
DB file 是MicroTSP 的数据文件,在TSP中用fetch得到。
epoh老师,DB结尾的数据文件在TSP中如何打开呢? ...
MicroTSP Databank Format
  http://econpy.googlecode.com/svn/trunk/pytrix/opendatabank.txt

?A simple example
?x.db  (x series:1,3,5,7,9)
?Undated Series
?Line 1 : starting index
?Line 2 : ending index
1
5
1
3
5
7
9
? save as x.db
??????
??????
?open x.db
fetch x;
print x;
? save as x.tsp
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-4-10 21:20:18
epoh 发表于 2012-4-10 21:07
MicroTSP Databank Format
  http://econpy.googlecode.com/svn/trunk/pytrix/opendatabank.txt
呵呵,谢谢epoh老师,可以显示
我开始用了dbprint命令,一直显示错误。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 12:06:28
epoh 发表于 2011-4-1 21:48
糊涂了,封掉a没发觉.
这问题,我再仔细看一下function rqss().
epoh 老师,我运行上边程序出现错误:

> xx1<- cbind(as(w,"matrix.diag.csr") %x% X,w %x% Z)
错误于cbind(deparse.level, ...) : args have differing numbers of rows
此外: 警告信息:
In as.matrix.csr(x, nrow = nrow, ncol = ncol, eps = eps) :
  ncol*nrow indivisable by length(x)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 12:18:45
xiaomuyoucun 发表于 2013-3-15 12:06
epoh 老师,我运行上边程序出现错误:

> xx1
哈哈,epoh老师太忙了
你可以直接利用rqpd 包了。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 12:45:03
ywh19860616 发表于 2013-3-15 12:18
哈哈,epoh老师太忙了
你可以直接利用rqpd 包了。
这个程序之前你也用过,那请问怎么回事?是哪里出问题了,你能修改过来吗?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 15:11:29
ywh19860616 发表于 2011-4-1 13:54
我试加了,程序也是可以运行的,只是结果不一样
> library(SparseM)
> library(quantreg)
你好,我发现这一段程序总是运行不过去,你之前运行过没有错误吗?为什么我的总出现以下错误?
急于求解:
> xx1<- cbind(as(w,"matrix.diag.csr") %x% X,w %x% Z)
错误于cbind(deparse.level, ...) : args have differing numbers of rows
此外: 警告信息:
In as.matrix.csr(x, nrow = nrow, ncol = ncol, eps = eps) :
  ncol*nrow indivisable by length(x)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 15:15:11
xiaomuyoucun 发表于 2013-3-15 15:11
你好,我发现这一段程序总是运行不过去,你之前运行过没有错误吗?为什么我的总出现以下错误?
急于求解 ...
你的最后目的是什么?运行面板分位回归模型吗?
如果答案是,那你直接利用rqpd包就可以了,作者
已经把这些程序打包为package了,可以直接用
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 15:15:53
xiaomuyoucun 发表于 2013-3-15 15:11
你好,我发现这一段程序总是运行不过去,你之前运行过没有错误吗?为什么我的总出现以下错误?
急于求解 ...
你是为了运行面板分位回归吗?
如果是,那用rqpd包就可以了
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 15:18:53
ywh19860616 发表于 2013-3-15 15:15
你是为了运行面板分位回归吗?
如果是,那用rqpd包就可以了
我不是很清楚这个包rqpd怎么用。。主要是我还实现惩罚项和工具变量
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 15:20:00
ywh19860616 发表于 2013-3-15 15:15
你是为了运行面板分位回归吗?
如果是,那用rqpd包就可以了
还有我不清楚为什么我运行那一段程序时总汇报那个错误,你当时没遇到吗?怎么解决的?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-3-15 15:30:36
xiaomuyoucun 发表于 2013-3-15 15:18
我不是很清楚这个包rqpd怎么用。。主要是我还实现惩罚项和工具变量
library(SparseM)
library(quantreg)
library(Ecdat)
data("Produc")  
x1=log(Produc$pcap)
x2=log(Produc$pc)
x3=log(Produc$emp)
x4=Produc$unemp
X=cbind(x1,x2,x3,x4)
y=log(Produc$gsp)
N<-length(y)
s= rep(1:48,rep(17,48))
X <- as.matrix(X)
p <- ncol(X)
n <- length(levels(as.factor(s)))
Z <- as.matrix.csr(model.matrix(~as.factor(s)-1))
lambda=1
w=c(0.1,0.25,0.5,0.25,0.1)
taus=c(0.1,0.25,0.5,0.75,0.9)
K <- length(w)
xx1<- cbind(as(w,"matrix.diag.csr") %x% X,as.matrix(w) %x% Z)
Penalty <- cbind(as.matrix.csr(0,n,K*p),lambda*as(n,"matrix.diag.csr"))
xx<-rbind(xx1,Penalty)
y <- c(w %x% y,rep(0,n))
a <- c((w*(1-taus)) %x% (t(X)%*%rep(1,N)),sum(w*(1-taus)) * (t(Z) %*% rep(1,N)) + lambda * rep(1/2,n))
rq.fit.sfn(xx,y,a)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-6-14 00:30:30
复制代码
复制代码

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

栏目导航
热门文章
推荐文章

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群