我想知道是否有任何简单的R命令或软件包都可以让我轻松地将变量添加到data.frames,这些是变量的"差异"或随时间的变化.
如果我的数据看起来像这样:
set.seed(1)
MyData <- data.frame(Day=0:9 %% 5+1,
Price=rpois(10,10),
Good=rep(c("apples","oranges"), each=5))
MyData
Day Price Good
1 1 8 apples
2 2 10 apples
3 3 7 apples
4 4 11 apples
5 5 14 apples
6 1 12 oranges
7 2 11 oranges
8 3 9 oranges
9 4 14 oranges
10 5 11 oranges
Run Code Online (Sandbox Code Playgroud)
然后在"第一次区分"价格变量后,我的数据将如下所示.
Day Price Good P1d
1 1 8 apples NA
2 2 10 apples 2
3 3 7 apples -3
4 4 11 apples 4 …Run Code Online (Sandbox Code Playgroud) 我有一个数据集,其中每一行都是一个固定的年份对,firmid其中一个是字符串。
如果我做
duplicates drop firmid year, force
Run Code Online (Sandbox Code Playgroud)
它不会删除任何内容,因为没有重复项(我最初在运行后创建了数据集duplicates drop firmid year, force)。
到目前为止,一切都很好。我想创建一个需要firmid数字的面板。所以我跑
egen newid = group(firmid)
xtset newid year
Run Code Online (Sandbox Code Playgroud)
但会弹出“面板中的重复时间值”错误。而且,
duplicates list newid year
Run Code Online (Sandbox Code Playgroud)
列出一大堆重复项。
似乎并egen, group()没有产生独特的群体。我的问题是:为什么以及如何以稳健的方式创建独特的群组?
我有一个如下所示的数据集:
我使用pandas.read_csv将"年份"和"国家/地区"列作为索引导入到pandas数据框中.我需要做的是将时间步长从每5年改为每年,并插入所述值,我真的不知道如何做到这一点.我正在学习R和python,所以对这两种语言的帮助都会受到高度赞赏.
We have a simple panel data set in long form, which has the following structure:
i t x
1 Aug-2011 282
2 Aug-2011 -220
1 Sep-2011 334
2 Sep-2011 126
1 Sep-2012 -573
2 Sep-2012 305
1 Nov-2013 335
2 Nov-2013 205
3 Nov-2013 485
Run Code Online (Sandbox Code Playgroud)
I would like to get the cross-correlation between each i within the time-variable t.
This would be possible by converting the data in wide format. Unfortunately, this approach is not feasible due to the …
我正在使用 Python 中的 statsmodels MixedLM 包估计混合线性模型。拟合模型后,我现在想进行预测,但正在努力理解“预测”方法。
statsmodels 文档 ( http://www.statsmodels.org/dev/generated/statsmodels.regression.mixed_linear_model.MixedLM.predict.html ) 表明 predict 方法采用包含已估计模型参数的数组。我怎样才能检索这个数组?
y = raw_data['dependent_var']
X = raw_data[['var1', 'var2', 'var3']]
groups = raw_data['person_id']
model = sm.MixedLM(endog=y, exog=X, groups=groups)
result = model.fit()
Run Code Online (Sandbox Code Playgroud) 我想知道你是否可以帮助我设计一种毫不费力的方法来编码我正在使用的这个国家/地区年度事件数据.
在下面的示例中,每一行都对应一个正在进行的事件(我最终将折叠成一个更广泛的面板数据集,这就是为什么它现在看起来很简陋).因此,例如,国家29在1920年开始发生事件,并在1921年继续(并结束).国家23在1921年开始了这一事件,持续到1923年.国家35开始了一个事件,发生在1921年,仅在1921年,等等.
country year
29 1920
29 1921
23 1921
23 1922
23 1923
35 1921
64 1926
135 1928
135 1929
135 1930
135 1931
135 1932
135 1933
135 1934
120 1930
70 1932
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建"开始"和"持续"变量.此示例数据框中的"正在进行的"变量很容易.基本上:Data$ongoing <- 1
我更感兴趣的是创建"起始"变量.如果它标志着给定国家的事件发生,它将被编码为1.基本上,我想创建一个看起来像这样的变量,给出这个示例数据.
country year onset
29 1920 1
29 1921 0
23 1921 1
23 1922 0
23 1923 0
35 1921 1
64 1926 1
135 1928 1
135 1929 0
135 1930 0
135 1931 0
135 1932 0 …Run Code Online (Sandbox Code Playgroud) 我正在使用 Stata 运行固定效应模型,然后执行样本外预测。但似乎
xtreg
Run Code Online (Sandbox Code Playgroud)
其次是
predict yhat, xbu
Run Code Online (Sandbox Code Playgroud)
不预测样本外以及固定效应。有没有办法通过包含固定效应将 xtreg 用于样本外?插图:
webuse nlswork
xtset idcode year
regress ln_wage age if year <= 80
predict temp1
xtreg ln_wage age if year <= 80, fe
predict temp2, xbu
Run Code Online (Sandbox Code Playgroud)
就我而言,我需要预测 year = 81 的值。并且 temp2 为空的年数 > 80。阅读 xtreg 和 areg 的手册,似乎不可能进行样本外预测,尤其是对于 xbu——其中包括固定效应预测。可以理解,如果我使用年份固定效果没有意义,但如果我只是使用idcode应该可以吗?任何建议将不胜感激。或者关于如何获得解决方案的任何建议?
它似乎只针对 <= 2000 年的所有年份生成。也就是说,我只能生成样本中的预测。
fixest我打算使用包的函数运行具有固定效应的工具变量回归feols。但是,我在没有进一步外源控制的情况下指定估计的语法存在问题。
考虑以下示例:
# Load package
require("fixest")
# Load data
df <- airquality
Run Code Online (Sandbox Code Playgroud)
我想要类似以下的内容,即通过仪器化内生变量和固定效应解释结果:
feols(Temp | Month + Day | Ozone ~ Wind, df)
Run Code Online (Sandbox Code Playgroud)
然而,这会产生一个错误:
The dependent variable is a constant. Estimation cannot be done.
Run Code Online (Sandbox Code Playgroud)
仅当我添加更多外生协变量时(如文档示例中所示),它才有效:
feols(Temp ~ Solar.R | Month + Day | Ozone ~ Wind, df)
Run Code Online (Sandbox Code Playgroud)
我该如何解决?在没有进一步控制的情况下,如何运行估算,例如Solar.R在本例中?
注意:我将其发布在 Stack Overflow 上而不是 Cross Validated 上,因为该问题涉及编码语法问题,而不是估计背后的计量经济学技术。
我需要创建一个变量来告诉我自第一次观察特定组conflictID以来的年数。我提供了一个示例数据集来说明我的问题。
conflictID <- c(205,205,205,209,209,221,221,221,221)
year <- c("1993", "1995", "1996", "1991", "1993", "2001", "2002", "2003", "2005")
df <- data.frame(conflictID, year)
Run Code Online (Sandbox Code Playgroud)
该数据帧的输出是:
conflictID year
1 205 1993
2 205 1995
3 205 1996
4 209 1991
5 209 1993
6 221 2001
7 221 2002
8 221 2003
9 221 2005
Run Code Online (Sandbox Code Playgroud)
我想要看起来像这样的东西:
conflictID year duration
1 205 1993 0
2 205 1995 2
3 205 1996 3
4 209 1991 0
5 209 1993 2
6 221 2001 0
7 221 …Run Code Online (Sandbox Code Playgroud) 我有以下数据:
country <- c("afg", "alb", "aus")
gdp_2018 <- c(123, 532, 555)
gdp_2019 <- c(233, 531, 621)
gdp_2020 <- c(112, 231, 323)
inf_2018 <- c(0.1, 0.2, 0.3)
inf_2019 <- c(0.01, 0.01, 0.2)
inf_2020 <- c(0.5, 0.4, 0.4)
df <- cbind.data.frame(country, gdp_2018, gdp_2019, gdp_2020,
inf_2018, inf_2019, inf_2020)
Run Code Online (Sandbox Code Playgroud)
我想将这种宽数据转换为长格式,如下所示:
country <- c("afg","afg","afg", "alb","alb","alb", "aus", "aus", "aus")
year <- c(2018, 2019, 2020, 2018, 2019, 2020, 2018, 2019, 2020)
gdp <- c(123, 532, 555,
233, 531, 621,112, 231, 323)
inf <- c(0.1, 0.2, 0.3, 0.01, …Run Code Online (Sandbox Code Playgroud) panel-data ×10
r ×5
stata ×3
dataframe ×2
python ×2
correlation ×1
date ×1
datediff ×1
melt ×1
mixed-models ×1
panel ×1
predict ×1
python-3.x ×1
regression ×1
reshape ×1
statsmodels ×1
tidyr ×1
time-series ×1