如何在 R 中索引预测 plm 对象
How to index predict plm object in R
在 plm 对象上使用 predict() 函数给出固定效应模型中每个实体的样本预测值。
如何索引生成的双对象以获得仅一个实体的预测值?
给出的例子:
library(plm)
data("Produc", package = "plm")
zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
data = Produc, index = c("state","year"))
yy=predict(zz)
# this returns only the first element of the ARIZONA entity prediction.
yy['ARIZONA']
#Output from above code
> yy['ARIZONA']
ARIZONA
-0.4264009
#Expected output:
> yy
ALABAMA ALABAMA ALABAMA ALABAMA ALABAMA
-0.2361898231 -0.2189895892 -0.1696057318 -0.1115933089 -0.0837388837
ALABAMA ALABAMA ALABAMA ALABAMA ALABAMA
-0.0927599459 -0.0162445537 0.0236004000 0.0771475623 0.0973429273
ALABAMA ALABAMA ALABAMA ALABAMA ALABAMA
0.0956672093 0.0784827429 0.0501708544 0.0640040527 0.1157573807
ALABAMA ALABAMA ARIZONA ARIZONA ARIZONA
0.1541848055 0.1727639012 -0.4264009370 -0.3666204604 -0.2707038121
ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
-0.1809125087 -0.1410211123 -0.1802185768 -0.1477399971 -0.0839823046
ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
0.0138358070 0.0985224005 0.1273115184 0.1711627777 0.1495094189
ARIZONA ARIZONA ARIZONA ARIZONA
0.1919410264 0.2873534379 0.3458664549 0.4120968673
我的问题是,如何操作 predict() 对象来获取我的一个实体的完整预测值集?
您的代码不明确,因此请检查 names
,它在括号内给出了一个布尔值。
yy[names(yy) %in% "ARIZONA"]
# ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
# -0.42640094 -0.36662046 -0.27070381 -0.18091251 -0.14102111 -0.18021858
# ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
# -0.14774000 -0.08398230 0.01383581 0.09852240 0.12731152 0.17116278
# ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
# 0.14950942 0.19194103 0.28735344 0.34586645 0.41209687
在 plm 对象上使用 predict() 函数给出固定效应模型中每个实体的样本预测值。
如何索引生成的双对象以获得仅一个实体的预测值?
给出的例子:
library(plm)
data("Produc", package = "plm")
zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
data = Produc, index = c("state","year"))
yy=predict(zz)
# this returns only the first element of the ARIZONA entity prediction.
yy['ARIZONA']
#Output from above code
> yy['ARIZONA']
ARIZONA
-0.4264009
#Expected output:
> yy
ALABAMA ALABAMA ALABAMA ALABAMA ALABAMA
-0.2361898231 -0.2189895892 -0.1696057318 -0.1115933089 -0.0837388837
ALABAMA ALABAMA ALABAMA ALABAMA ALABAMA
-0.0927599459 -0.0162445537 0.0236004000 0.0771475623 0.0973429273
ALABAMA ALABAMA ALABAMA ALABAMA ALABAMA
0.0956672093 0.0784827429 0.0501708544 0.0640040527 0.1157573807
ALABAMA ALABAMA ARIZONA ARIZONA ARIZONA
0.1541848055 0.1727639012 -0.4264009370 -0.3666204604 -0.2707038121
ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
-0.1809125087 -0.1410211123 -0.1802185768 -0.1477399971 -0.0839823046
ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
0.0138358070 0.0985224005 0.1273115184 0.1711627777 0.1495094189
ARIZONA ARIZONA ARIZONA ARIZONA
0.1919410264 0.2873534379 0.3458664549 0.4120968673
我的问题是,如何操作 predict() 对象来获取我的一个实体的完整预测值集?
您的代码不明确,因此请检查 names
,它在括号内给出了一个布尔值。
yy[names(yy) %in% "ARIZONA"]
# ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
# -0.42640094 -0.36662046 -0.27070381 -0.18091251 -0.14102111 -0.18021858
# ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
# -0.14774000 -0.08398230 0.01383581 0.09852240 0.12731152 0.17116278
# ARIZONA ARIZONA ARIZONA ARIZONA ARIZONA
# 0.14950942 0.19194103 0.28735344 0.34586645 0.41209687