【问题标题】:Removing a custom (second) class from a dataset/variable从数据集/变量中删除自定义(第二个)类
【发布时间】:2019-08-18 08:54:17
【问题描述】:

我一直在使用hmisc 包中的一个类,名为haven_labelled(或者有时只是labelled)。其目的是从 Stata .dta 数据集中导入列标签。尝试在数据帧上使用 plm 时出现错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘c("pseries", "haven_labelled")’ to a data.frame

类如下:

> class(actualdataset)
[1] "pdata.frame" "data.frame"
> class(actualdataset$examplevar)
[1] "pseries"        "haven_labelled"

因此,我想从该数据库中删除 haven_labelled 类。我很遗憾无法重现错误。我认为这与我的actualdataset 中的var 属于双类有关,其中包括haven_labelled。请参阅以下示例数据集。

library(data.table)
library(plm)
library(Hmisc)
set.seed(1)
DT <- data.table(panelID = sample(50,50),                                                    # Creates a panel ID
                      Country = c(rep("A",30),rep("B",50), rep("C",20)),       
                      some_NA = sample(0:5, 6),                                             
                      some_NA_factor = sample(0:5, 6),         
                      Group = c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20)),
                      Time = rep(seq(as.Date("2010-01-03"), length=20, by="1 month") - 1,5),
                      norm = round(runif(100)/10,2),
                      Income = sample(100,100),
                      Happiness = sample(10,10),
                      Sex = round(rnorm(10,0.75,0.3),2),
                      Age = round(rnorm(10,0.75,0.3),2),
                      Educ = round(rnorm(10,0.75,0.3),2))           
DT [, uniqueID := .I]                                                                        # Creates a unique ID     
DT[DT == 0] <- NA                                                                            # https://stackoverflow.com/questions/11036989/replace-all-0-values-to-na
DT$some_NA_factor <- factor(DT$some_NA_factor)
labels <- data.table::fread("Varcode Variables
                         panelID a
                         Country b
                         Group c
                         Time d
                         norm e
                         Income f
                         Happiness g
                         Sex h
                         Age i
                         Educ j
                         uniqueID k                         
                         ", header = TRUE)
for (i in seq_len(ncol(DT))) { 
    label(DT[[i]]) <-  labels$Variables[match(names(DT)[i], labels$Varcode)] 
 }
DTp <- plm::pdata.frame(DT, index= c("panelID", "Time"))
result <- plm(Happiness ~ Income, data=DTp, model="within")

> class(DTp)
[1] "pdata.frame" "data.frame"
> class(DTp$Income)
[1] "pseries"  "labelled" "integer" 

有什么建议吗?

编辑:我在想如下的事情:

for for (i in seq_len(ncol(DT)) {
    if (sapply(DT, function(x) class(x)[1L]) == "haven_labelled") { 
        attr(DT[,i],"class[1L]") <- "integer"
    }
 }

编辑 2:答案在应用 plm 时防止了任何错误。遗憾的是,所有coefficientsstandard errors 都为零。 P-valuest-valuesNA。我不确定是什么原因造成的。

【问题讨论】:

  • 如果你运行:class(DTp$Income) &lt;- "pseries" 会发生什么?
  • 检查attributes(DTp$Income)attributes(DTp$Income)$classattr(DTp$Income,"class"),在这种情况下attr(DTp$Income,"class") &lt;- c("pseries","integer")可能会有所帮助。
  • @A.Suliman 感谢您的评论。我正在寻找一种更通用的解决方案,可以在整个(实际)数据集上使用。我编辑了原始帖子以更好地解释我的期望。你介意看看吗?
  • @Ben Nutzer 感谢您的评论。我用整个数据集尝试了你的方法,但把它变成了一个 peries 列表。

标签: r class plm hmisc


【解决方案1】:

本方案基于提供的数据集DTp,根据你原来的数据集更改labelledlabelled_ch

for (i in seq_len(ncol(DTp))) {
  if (any(class(DTp[,i]) == "labelled")) {
    #browser()
    ind = which(class(DTp[,i])=="labelled")
    attr(DTp[,i],"class")[ind] <- "labelled_ch"
  }
}

【讨论】:

  • 非常感谢!我现在顺利通过了plm 阶段。
  • 查看第二次编辑。我不确定它与原始问题有关,这就是我删除它的原因。 lm 仍然有效..
  • 啊,很高兴知道!然而,对于示例数据集,我不确定这是否令人惊讶,因为我没有对数据进行任何思考。然而,我现在已经能够通过plm 和实际数据集获得结果。我认为我必须更加小心我投入回归的内容。非常感谢您的帮助!
猜你喜欢
  • 2014-01-30
  • 1970-01-01
  • 2013-01-12
  • 2018-09-26
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
相关资源
最近更新 更多