2011-08-19 74 views
10

Tôi có một bảng điều khiển T lớn nhỏ mà tôi đang ước tính qua plm (mô hình hồi quy tuyến tính bảng), với các hiệu ứng cố định.Có chức năng dự đoán cho PLM trong R không?

Có cách nào để nhận giá trị dự đoán cho tập dữ liệu mới không? (Tôi muốn ước tính tham số trên một tập hợp con của mẫu của tôi và sau đó sử dụng các tham số này để tính giá trị ngụ ý mô hình cho toàn bộ mẫu).

Cảm ơn!

+0

Có vẻ như bạn đang sử dụng 'lm' dưới mui xe, vì vậy bạn đã thử gọi hàm' predict.lm' chưa? – James

+2

Tôi nghi ngờ các tác giả biết rằng việc phát hành chức năng 'predict định.plm' sẽ khuyến khích những người không hiểu các vấn đề thống kê để áp dụng một cách mù quáng khi các giả định không được đáp ứng. IIRC, gói lme4 không cung cấp chức năng dự đoán hoặc các tác giả plm lưu ý rằng chúng đang mang cả hai thành phần ngẫu nhiên và cố định. –

+0

predict đoán.lm không hoạt động. Tôi cho rằng có một cách để trích xuất các hệ số và chặn nhưng tôi tưởng tượng những người khác đã gặp phải vấn đề này –

Trả lời

7

Có (ít nhất) hai phương pháp trong gói để sản xuất ước tính từ các đối tượng plm:

- fixef.plm: Extract Effects Cố định

- pmodel.response: Một chức năng để giải nén model.response

Dường như với tôi rằng tác giả không quan tâm đến việc cung cấp ước tính cho "hiệu ứng ngẫu nhiên". Nó có thể là một vấn đề "nếu bạn không biết làm thế nào để làm điều đó một mình, sau đó chúng tôi không muốn cung cấp cho bạn một con dao sắc nét để cắt chính mình quá sâu sắc."

2

Tôi đã viết một chức năng gọi là predict.out.plm có thể tạo ra những dự đoán cho gốc dữ liệu và cho một thao túng bộ dữ liệu (với tên cột bằng nhau).

predict.out.plm tính a) kết quả được dự đoán (được trang bị) của dữ liệu được chuyển đổi và b) tạo kết quả theo cấp độ. Hàm này làm việc cho các ước tính khác biệt đầu tiên (FD) và ước tính hiệu ứng cố định (FE) bằng cách sử dụng plm. Đối với FD nó tạo ra kết quả khác biệt theo thời gian và đối với FE nó tạo ra kết quả theo thời gian.

Chức năng này hầu như chưa được kiểm tra và có thể chỉ hoạt động với khung dữ liệu cân bằng mạnh.

Mọi đề xuất và chỉnh sửa đều được hoan nghênh. Giúp phát triển một gói R nhỏ sẽ rất được đánh giá cao.

Chức năng predict.out.plm

predict.out.plm<-function(
    estimate, 
    formula, 
    data, 
    model="fd", 
    pname="y", 
    pindex=NULL, 
    levelconstr=T 
){ 
    # estimate=e.fe 
    # formula=f 
    # data=d 
    # model="within" 
    # pname="y" 
    # pindex=NULL 
    # levelconstr=T 
    #get index of panel data 
    if (is.null(pindex) && class(data)[1]=="pdata.frame") { 
    pindex<-names(attributes(data)$index) 
    } else { 
    pindex<-names(data)[1:2] 
    } 
    if (class(data)[1]!="pdata.frame") { 
    data<-pdata.frame(data) 
    } 
    #model frame 
    mf<-model.frame(formula,data=data) 
    #model matrix - transformed data 
    mn<-model.matrix(formula,mf,model) 

    #define variable names 
    y.t.hat<-paste0(pname,".t.hat") 
    y.l.hat<-paste0(pname,".l.hat") 
    y.l<-names(mf)[1] 

    #transformed data of explanatory variables 
    #exclude variables that were droped in estimation 
    n<-names(estimate$aliased[estimate$aliased==F]) 
    i<-match(n,colnames(mn)) 
    X<-mn[,i] 

    #predict transformed outcome with X * beta 
    # p<- X %*% coef(estimate) 
    p<-crossprod(t(X),coef(estimate)) 
    colnames(p)<-y.t.hat 

    if (levelconstr==T){ 
    #old dataset with original outcome 
    od<-data.frame(
     attributes(mf)$index, 
     data.frame(mf)[,1] 
    ) 
    rownames(od)<-rownames(mf) #preserve row names from model.frame 
    names(od)[3]<-y.l 

    #merge old dataset with prediciton 
    nd<-merge(
     od, 
     p, 
     by="row.names", 
     all.x=T, 
     sort=F 
    ) 
    nd$Row.names<-as.integer(nd$Row.names) 
    nd<-nd[order(nd$Row.names),] 

    #construct predicted level outcome for FD estiamtions 
    if (model=="fd"){ 
     #first observation from real data 
     i<-which(is.na(nd[,y.t.hat])) 
     nd[i,y.l.hat]<-NA 
     nd[i,y.l.hat]<-nd[i,y.l] 
     #fill values over all years 
     ylist<-unique(nd[,pindex[2]])[-1] 
     ylist<-as.integer(as.character(ylist)) 
     for (y in ylist){ 
     nd[nd[,pindex[2]]==y,y.l.hat]<- 
      nd[nd[,pindex[2]]==(y-1),y.l.hat] + 
      nd[nd[,pindex[2]]==y,y.t.hat] 
     } 
    } 
    if (model=="within"){ 
     #group means of outcome 
     gm<-aggregate(nd[, pname], list(nd[,pindex[1]]), mean) 
     gl<-aggregate(nd[, pname], list(nd[,pindex[1]]), length) 
     nd<-cbind(nd,groupmeans=rep(gm$x,gl$x)) 
     #predicted values + group means 
     nd[,y.l.hat]<-nd[,y.t.hat] + nd[,"groupmeans"] 
    } 
    if (model!="fd" && model!="within") { 
     stop('funciton works only for FD and FE estimations') 
    } 
    } 
    #results 
    results<-p 
    if (levelconstr==T){ 
    results<-list(results,nd) 
    names(results)<-c("p","df") 
    } 
    return(results) 
} 

kiểm tra các chức năng:

##packages 
library(plm) 

##test dataframe 
#data structure 
N<-4 
G<-2 
M<-5 
d<-data.frame(
    id=rep(1:N,each=M), 
    year=rep(1:M,N)+2000, 
    gid=rep(1:G,each=M*2) 
) 
#explanatory variable 
d[,"x"]=runif(N*M,0,1) 
#outcome 
d[,"y"] = 2 * d[,"x"] + runif(N*M,0,1) 
#panel data frame 
d<-pdata.frame(d,index=c("id","year")) 

##new data frame for out of sample prediction 
dn<-d 
dn$x<-rnorm(nrow(dn),0,2) 

##estimate 
#formula 
f<- pFormula(y ~ x + factor(year)) 
#fixed effects or first difffernce estimation 
e<-plm(f,data=d,model="within",index=c("id","year")) 
e<-plm(f,data=d,model="fd",index=c("id","year")) 
summary(e) 

##fitted values of estimation 
#transformed outcome prediction 
predict(e) 
c(pmodel.response(e)-residuals(e)) 
predict.out.plm(e,f,d,"fd")$p 
# "level" outcome prediciton 
predict.out.plm(e,f,d,"fd")$df$y.l.hat 
#both 
predict.out.plm(e,f,d,"fd") 

##out of sampel prediciton 
predict(e,newdata=d) 
predict(e,newdata=dn) 
# Error in crossprod(beta, t(X)) : non-conformable arguments 
# if plm omits variables specified in the formula (e.g. one year in factor(year)) 
# it tries to multiply two matrices with different length of columns than regressors 
# the new funciton avoids this and therefore is able to do out of sample predicitons 
predict.out.plm(e,f,dn,"fd") 
Các vấn đề liên quan