2011-08-19 16 views
10

मेरे पास एक छोटा एन बड़ा टी पैनल है जिसे मैं निश्चित प्रभाव के साथ पीएलएम (पैनल रैखिक रिग्रेशन मॉडल) के माध्यम से अनुमान लगा रहा हूं।क्या आर में पीएलएम के लिए कोई पूर्वानुमान कार्य है?

क्या कोई नया डेटासेट के लिए अनुमानित मान प्राप्त करने का कोई तरीका है? (मैं अपने नमूने के सबसेट पर पैरामीटर अनुमानित करना चाहता हूं, और फिर इन्हें पर पूरे नमूने के लिए मॉडल-अंतर्निहित मानों की गणना करें)।

धन्यवाद!

+0

ऐसा लगता है कि हुड के तहत 'एलएम' का उपयोग किया जा रहा है, तो क्या आपने 'predict.lm' को कॉल करने का प्रयास किया है? – James

+2

मुझे संदेह है कि लेखकों को पता है कि 'predict.plm' फ़ंक्शन जारी करने से उन लोगों को प्रोत्साहित किया जाएगा जो धारणाओं को पूरा नहीं होने पर सांख्यिकीय मुद्दों को अंधाधुंध रूप से लागू करने के लिए समझते हैं। आईआईआरसी, एलएम 4 पैकेज या तो भविष्यवाणी समारोह प्रदान नहीं करता है और पीएलएम लेखकों ने नोट किया है कि वे दोनों यादृच्छिक और निश्चित घटकों का अनुमान लगा रहे हैं। –

+0

predict.lm काम नहीं करता है। मुझे लगता है कि गुणांक और अंतःक्रियाओं को निकालने का एक तरीका है, लेकिन मुझे लगता है कि दूसरों को पहले से ही इस मुद्दे का सामना करना पड़ा है –

उत्तर

7

रहे हैं (कम से कम) पैकेज PLM वस्तुओं से अनुमान तैयार करने में दो तरीकों:

- fixef.plm: फिक्स्ड प्रभाव

निकालने - pmodel.response: एक समारोह को निकालने के लिए model.response

ऐसा लगता है कि लेखक "यादृच्छिक प्रभाव" के अनुमान प्रदान करने में रूचि नहीं रखते हैं। यह एक मामला हो सकता है "यदि आप नहीं जानते कि इसे अपने आप कैसे करें, तो हम आपको बहुत गहराई से कटौती करने के लिए एक तेज चाकू नहीं देना चाहते हैं।"

2

मैं एक समारोह predict.out.plm कहा जाता है कि मूल डेटा के लिए और एक चालाकी से डेटा सेट के लिए भविष्यवाणियों (बराबर स्तंभ नाम के साथ) बना सकते हैं लिखा था।

predict.out.plm गणना करता है ए) परिवर्तित डेटा के अनुमानित (फिट) परिणाम और बी) स्तर के परिणाम के अनुसार बनाता है। यह समारोह plm का उपयोग कर प्रथम अंतर (एफडी) अनुमानों और फिक्स्ड इफेक्ट्स (एफई) अनुमानों के लिए काम करता है। एफडी के लिए यह समय के साथ अलग-अलग परिणाम बनाता है और एफई के लिए यह समय-व्यर्थ परिणाम बनाता है।

फ़ंक्शन काफी हद तक अनचाहे है, और शायद केवल दृढ़ता से संतुलित डेटा फ्रेम के साथ काम करता है।

कोई सुझाव और सुधार बहुत स्वागत है। एक छोटे आर पैकेज को विकसित करने में मदद की बहुत सराहना की जाएगी।

समारोह 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) 
} 

परीक्षण समारोह:

##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") 
संबंधित मुद्दे