Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中带有{plm}的模型中使用所有变量_R_Cluster Analysis_Linear Regression_Plm - Fatal编程技术网

在R中带有{plm}的模型中使用所有变量

在R中带有{plm}的模型中使用所有变量,r,cluster-analysis,linear-regression,plm,R,Cluster Analysis,Linear Regression,Plm,使用不同的来源,我编写了一个小函数,它创建了一个包含标准误差、t统计数据和标准误差的表,这些数据在线性回归模型之后根据组变量“cluster”进行聚类。代码如下 cl1 <- function(modl,clust) { # model is the regression model # clust is the clustervariable # id is a unique identifier in ids library(plm) library(lmtest

使用不同的来源,我编写了一个小函数,它创建了一个包含标准误差、t统计数据和标准误差的表,这些数据在线性回归模型之后根据组变量“cluster”进行聚类。代码如下

cl1 <- function(modl,clust) {
 # model is the regression model
 # clust is the clustervariable
 # id is a unique identifier in ids
    library(plm)
    library(lmtest)
        #  Get Formula
    form <- formula(modl$call)
        # Get Data frame
    dat <- eval(modl$call$data)
    dat$row <- rownames(dat)
    dat$id <- ave(dat$row, dat[[deparse(substitute(clust))]], FUN =seq_along)       
    pdat <- pdata.frame(dat, 
         index=c("id", deparse(substitute(clust)))
         , drop.index= F, row.names= T)
    # # Regression
      reg <- plm(form, data=pdat, model="pooling")  
    # # Adjustments
     G <- length(unique(dat[, deparse(substitute(clust))]))
     N <- length(dat[,deparse(substitute(clust))])
    # # Resid degrees of freedom, adjusted
     dfa <- (G/(G-1))*(N-1)/reg$df.residual
     d.vcov <- dfa* vcovHC(reg, type="HC0", cluster="group", adjust=T)
    table <- coeftest(reg, vcov=d.vcov)
    # #  Output: se, t-stat and p-val
     cl1out <- data.frame(table[, 2:4])
     names(cl1out) <- c("se", "tstat", "pval")
    # # Cluster VCE
     return(cl1out)

经过一些测试,我猜我不能使用“.”来表示{plm}使用数据帧中的所有变量。有没有办法用{plm}来实现这一点?否则,有没有关于如何以一种不使用{plm}并且接受线性模型的所有可能规范的方式改进我的函数的想法

事实上,您不能在
plm
pacakge中对公式使用
符号

data("Produc", package = "plm")
plm(gsp ~ .,data=Produc)
Error in terms.formula(object) : '.' in formula and no 'data' argument
一个想法是,当您有一个
时,扩展公式。下面是一个执行此任务的自定义函数(当然是在其他包中完成的):


按您设置的方式展开公式,
y~。
将(尝试…)包括
pdat
的所有列,除了
y
,但包括
id
clust
。你确定这就是你想做的吗?哦,谢谢!试图解决我的“bug”我没见过这个!
data("Produc", package = "plm")
plm(gsp ~ .,data=Produc)
Error in terms.formula(object) : '.' in formula and no 'data' argument
expand_formula <- 
  function(form="A ~.",varNames=c("A","B","C")){
  has_dot <- any(grepl('.',form,fixed=TRUE))
  if(has_dot){
    ii <- intersect(as.character(as.formula(form)),
          varNames)
    varNames <- varNames[!grepl(paste0(ii,collapse='|'),varNames)]

   exp <- paste0(varNames,collapse='+')
   as.formula(gsub('.',exp,form,fixed=TRUE))

  }
  else as.formula(form)
}
(eform = expand_formula("gsp ~ .",names(Produc)))
#    gsp ~ state + year + pcap + hwy + water + util + pc + emp + unemp

plm(eform,data=Produc)

# Model Formula: gsp ~ state + year + pcap + hwy + water + util + pc + emp + unemp
# <environment: 0x0000000014c3f3c0>