gpt4 book ai didi

R plm 认为我的数字向量是一个因素,为什么?

转载 作者:行者123 更新时间:2023-12-02 03:08:27 25 4
gpt4 key购买 nike

有了这个数据输入:

A   B   C   D
0.0513748973337 0.442624990365 0.044669941640565 12023787.0495
-0.047511808790502 0.199057057555 0.067542653775225 6674747.75598
0.250333519823608 0.0400359422093 -0.062361320324768 10836244.44
0.033600922318947 0.118359141703 0.048493523722074 7521473.94034
0.00492552770819 0.0851342003243 0.027123088894137 8742685.39098
0.02053037069955 0.0535545969759 0.06352586720282 8442677.4204
0.09050961131549 0.044871795257 0.049363888991624 7223126.70424
0.082789930841618 0.0230375009412 0.090676778601245 8974611.5623
0.06396481119371 0.0467280364963 0.128097065131764 8167179.81463

和这段代码:

library(plm);
mydata <- read.csv("reproduce_small.csv", sep = "\t");
plm(C ~ log(D), data = mydata, model = "pooling"); # works
plm(A ~ log(B), data = mydata, model = "pooling"); # error

第二个plm调用返回以下错误:

Error in Math.factor(B) : ‘log’ not meaningful for factors

reproduce_small.csv包含上面粘贴的十行数据。显然,B不是一个因素,它显然是一个数值向量。这意味着 plm认为是一个因素。问题是“为什么?”,但更重要的是“我该如何解决这个问题?”

我尝试过的事情:

#1) mydata$B.log <- log(mydata$B)结果在

Error in model.frame.default(formula = y ~ X - 1, drop.unused.levels = TRUE) : 
variable lengths differ (found for 'X')

这本身就很奇怪,因为 A 和 B.log 的长度显然相同。

#2) plm(A ~ log(D), data = mydata, model = "pooling");导致与 #1 相同的错误。

#3) plm(C ~ log(B), data = mydata, model = "pooling");导致相同的原始错误(日志对因素没有意义)。

#4) plm(A ~ log(B + 1), data = mydata, model = "pooling");结果

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels
In addition: Warning message:
In Ops.factor(B, 1) : ‘+’ not meaningful for factors

#5) plm(A ~ as.numeric(as.character(log(B))), data = mydata, model = "pooling");导致相同的原始错误(日志对因素没有意义)。

编辑:按照建议,我包括了 str(mydata) 的结果:

> str(mydata)
'data.frame': 9 obs. of 4 variables:
$ A: num 0.05137 -0.04751 0.25033 0.0336 0.00493 ...
$ B: num 0.4426 0.1991 0.04 0.1184 0.0851 ...
$ C: num 0.0447 0.0675 -0.0624 0.0485 0.0271 ...
$ D: num 12023787 6674748 10836244 7521474 8742685 ...

也在尝试 mydata <- read.csv("reproduce_small.csv", sep = "\t", stringsAsFactors = FALSE);没用。

最佳答案

Helix123 在评论中指出data.frame 应该转换为pdata.frame。因此,例如,这个玩具示例的解决方案是:

mydata$E <- c("x", "x", "x", "x", "x", "y", "y", "y", "y"); # Create E as an "index"
mydata <- pdata.frame(mydata, index = "E"); # convert to pdata.frame
plm(A ~ log(B), data = mydata, model = "pooling"); # now it works!

编辑:至于“为什么”会发生这种情况,正如 Helix123 在评论中指出的那样,当传递 data.frame 而不是 pdata.frame 时,plm 悄悄地假定前两列是索引,并将它们转换为引擎盖下的因子。然后 plm 将抛出一个无用的错误,而不是发出一个警告,指出传递的对象类型不正确,或者它根本没有作出假设。

关于R plm 认为我的数字向量是一个因素,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com