我有一个使用以下命令创建的plm对象:
require(plm)
plm1 <- plm(Sepal.Length ~ Petal.Length + Petal.Width, data = iris, index = "Species")我正在尝试提取残差来手动计算物种的r平方,因为似乎不能将pseries对象操作成有用的东西,比如矩阵或data.frame。
> data.frame(resid(plm1))
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class '"pseries"' into a data.frame如果我有这样的东西就好了:
> df1 <- data.frame(time = rep(1:10,15), Species = iris$Species, resid1 = runif(150))
> head(df1)
time Species resid1
1 1 setosa 0.7038776
2 2 setosa 0.2164597
3 3 setosa 0.1988884
4 4 setosa 0.9311872
5 5 setosa 0.7087211
6 6 setosa 0.9914357我可以使用ddply或aggregate on来计算每个物种的r平方。
有什么建议吗?
发布于 2019-08-14 20:15:37
这是一个古老的问题,但我想指出一些容易遗漏并可能导致严重错误的东西。previous answer by dickoa是正确的,但我想澄清一下为什么需要这样的变通方法,因为它可能并不明显。
在阅读another thread时,我学到了以下几点:正如here所指出的,plm并不一定以给定给函数的顺序保存数据。这意味着,如果您不小心,简单地在plm-object上使用residuals()函数,然后将其连接到您的数据上,可能会导致将错误的残差分组到错误的数据行!作为示例,请考虑以下内容:
require(plm)
data("Gasoline") # The Gasoline dataset from the plm package
plm1 <- plm(lgaspcar ~ lincomep + lrpmg + lcarpcap, data=Gasoline, method = "within", index = c("country", "year"))
coef(plm1)
lincomep lrpmg lcarpcap
0.6622497 -0.3217025 -0.6404829
head(residuals(plm1))
1 2 3 4 5 6
-0.18814207 -0.19642727 -0.14874420 -0.12476346 -0.12114060 -0.08684045 注意我们得到的残差。现在,让我们只更改数据集的排序顺序。这应该不会改变分析中的任何内容。
set.seed(1234)
Gasoline2 <- Gasoline[order(runif(nrow(Gasoline))), ] # We just change the order of the rows.
plm2 <- plm(lgaspcar ~ lincomep + lrpmg + lcarpcap, data=Gasoline2, method = "within", index = c("country", "year"))
coef(plm2)
lincomep lrpmg lcarpcap
0.6622497 -0.3217025 -0.6404829
head(residuals(plm2))
258 7 64 73 268 186
-0.18814207 -0.19642727 -0.14874420 -0.12476346 -0.12114060 -0.08684045 乍一看,这似乎很好;估计的系数与以前相同。但是,请注意,显示残差的顺序与我们移动行之前的顺序相同。唯一改变的是,与残差相关联的名称现在反映了它们在数据中的新位置。因此,观察到后重新排序在数据中的行1上,是在行258上预先重新排序的。
Gasoline2[1, ]
country year lgaspcar lincomep lrpmg lcarpcap
258 SWEDEN 1970 3.989372 -7.73261 -2.733592 -8.164506
Gasoline[258, ]
country year lgaspcar lincomep lrpmg lcarpcap
258 SWEDEN 1970 3.989372 -7.73261 -2.733592 -8.164506这意味着,如果我们使用Gasoline2作为我们正在处理的数据集,那么在Gasoline2和residuals(plm2)上使用像cbind()这样的函数将导致错误的残差连接到观测值。
head(cbind(Gasoline, residuals(plm1)))
country year lgaspcar lincomep lrpmg lcarpcap residuals(plm1)
1 AUSTRIA 1960 4.173244 -6.474277 -0.3345476 -9.766840 -0.18814207
2 AUSTRIA 1961 4.100989 -6.426006 -0.3513276 -9.608622 -0.19642727
3 AUSTRIA 1962 4.073177 -6.407308 -0.3795177 -9.457257 -0.14874420
4 AUSTRIA 1963 4.059509 -6.370679 -0.4142514 -9.343155 -0.12476346
5 AUSTRIA 1964 4.037689 -6.322247 -0.4453354 -9.237739 -0.12114060
6 AUSTRIA 1965 4.033983 -6.294668 -0.4970607 -9.123903 -0.08684045
head(cbind(Gasoline2, residuals(plm2)))
country year lgaspcar lincomep lrpmg lcarpcap residuals(plm2)
258 SWEDEN 1970 3.989372 -7.732610 -2.7335921 -8.164506 -0.18814207
7 AUSTRIA 1966 4.047537 -6.252545 -0.4668377 -9.019822 -0.19642727
64 DENMARK 1966 4.233643 -5.851866 -0.3961885 -8.681541 -0.14874420
73 DENMARK 1975 4.033015 -5.612967 -0.3939543 -8.274632 -0.12476346
268 SWITZERL 1961 4.441330 -6.111640 -0.8655847 -9.158229 -0.12114060
186 JAPAN 1974 4.007964 -5.852553 -0.1909064 -8.846520 -0.08684045正如我们在上面看到的,在Gasoline2示例中,残差被分配到了错误的行。
那到底是怎么回事?好的,正如前面提到的,plm不会保持观察的顺序。使用前面答案中指出的attr()函数dickoa,我们可以看到plm按国家和年份重新组织数据。
head( attr(residuals(plm2), "index") )
country year
1 AUSTRIA 1960
2 AUSTRIA 1961
3 AUSTRIA 1962
4 AUSTRIA 1963
5 AUSTRIA 1964
6 AUSTRIA 1965这就是原始汽油数据的结构,这就是为什么残差以相同的顺序显示。
因此,我们可以使用attr(residuals(plm2), "index")向我们提供残差及其相应的国家和年份指标的事实,以便将残差添加到原始数据中。正如here所指出的,plyr包对此非常有帮助。
require(plyr)
resids2 <- data.frame(residual = residuals(plm2), attr(residuals(plm2), "index"))
Gasoline2$year <- factor(Gasoline2$year) # Needed since resids2$year is a factor, and Gasoline2$years was an integer. plyr does not accept them to be of different types.
Gasoline2 <- join(Gasoline2, resids2, by = c("country", "year"))
head(Gasoline2)
country year lgaspcar lincomep lrpmg lcarpcap residual
1 SWEDEN 1970 3.989372 -7.732610 -2.7335921 -8.164506 -0.02468148
2 AUSTRIA 1966 4.047537 -6.252545 -0.4668377 -9.019822 -0.02479759
3 DENMARK 1966 4.233643 -5.851866 -0.3961885 -8.681541 0.03175032
4 DENMARK 1975 4.033015 -5.612967 -0.3939543 -8.274632 -0.06575219
5 SWITZERL 1961 4.441330 -6.111640 -0.8655847 -9.158229 -0.05789130
6 JAPAN 1974 4.007964 -5.852553 -0.1909064 -8.846520 -0.21957156这给了我们正确的结果。
发布于 2014-08-05 05:56:32
也许这些思路上的一些东西能起到作用
library(plm)
plm1 <- plm(Sepal.Length ~ Petal.Length + Petal.Width, data = iris, index = "Species")
res <- residuals(plm1)
df <- cbind(as.vector(res), attr(res, "index"))
names(df) <- c("resid", "species", "time")
str(df)
## 'data.frame': 150 obs. of 3 variables:
## $ resid : num 0.1499 -0.0501 -0.1595 -0.4407 0.0499 ...
## $ species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ time : Factor w/ 50 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...https://stackoverflow.com/questions/25127840
复制相似问题