我需要得到特定行(这里:年)的一列(这里是:得分)的平均值.具体来说,我想知道三个时期的平均分数:
这是我数据的结构:
country year score
Algeria 1980 -1.1201501
Algeria 1981 -1.0526943
Algeria 1982 -1.0561565
Algeria 1983 -1.1274560
Algeria 1984 -1.1353926
Algeria 1985 -1.1734330
Algeria 1986 -1.1327666
Algeria 1987 -1.1263586
Algeria 1988 -0.8529455
Algeria 1989 -0.2930265
Algeria 1990 -0.1564207
Algeria 1991 -0.1526328
Algeria 1992 -0.9757842
Algeria 1993 -0.9714060
Algeria 1994 -1.1422258
Algeria 1995 -0.3675797
...
Run Code Online (Sandbox Code Playgroud)
计算出的平均值应添加到附加列中的df("平均值"),即第1期的相同平均值,第2期等.
它应该是这样的:
country year score mean
Algeria 1980 -1.1201501 -1.089
Algeria 1981 -1.0526943 -1.089
Algeria 1982 -1.0561565 …Run Code Online (Sandbox Code Playgroud) 我试图在多重估算数据上运行一个多级模型(用Amelia创建); 样本基于群集样本,群组= 24,N = 150.
library("ZeligMultilevel")
ML.model.0 <- zelig(dv~1 + tag(1|group), model="ls.mixed",
data=a.out$imputations)
summary(ML.model.0)
Run Code Online (Sandbox Code Playgroud)
此代码生成以下错误代码:
Error in object[[1]]$result$call :
$ operator not defined for this S4 class
Run Code Online (Sandbox Code Playgroud)
如果我运行OLS回归,它可以工作:
model.0 <- zelig(dv~1, model="ls", data=a.out$imputations)
m.0 <- coef(summary(model.0))
print(m.0, digits = 2)
Value Std. Error t-stat p-value
[1,] 45 0.34 130 2.6e-285
Run Code Online (Sandbox Code Playgroud)
我很高兴提供一个有效的例子.
require(Zelig)
require(Amelia)
require(ZeligMultilevel)
data(freetrade)
length(freetrade$country) #grouping variable
#Imputation of missing data
a.out <- amelia(freetrade, m=5, ts="year", cs="country")
# Models: (1) OLS; (2) multi-level
model.0 <- zelig(polity~1, model="ls", …Run Code Online (Sandbox Code Playgroud) 为了在匹配过程之前和之后直观地显示协变量平衡,我编写了以下代码:
library(lattice)
library(gridExtra)
StandBias.df= data.frame(
Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
Groups = factor(c(rep("0", 6), rep("1", 6))),
Values = c(0.26, 0.4, 0.3, 0.21, 0.6, 0.14, 0.12, -0.04, -0.23, 0.08, 0.14, -0.27))
d1<-dotplot(Category ~Values, data = StandBias.df, groups = Groups,
main = "Standardized Mean Differences", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
key=list(text=list(c("Pre-Matching", "Post-Matching")),
points=list(col = c("black", "grey50"), pch=c(22,15)),
space="bottom", border=T))
Ttest.df = data.frame(
Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
Groups = factor(c(rep("0", 6), rep("1", 6))),
Values = …Run Code Online (Sandbox Code Playgroud) Imagine you have yearly data for some sort of expenses. You are interested in the percent difference between the first value (t0) and each subsequent value (t1, ... -> tx) BUT only for a specific group of observations, i.e. with the next group, a new series of subsequent years starts.
Example:
value <- c(10225,10287,10225,10087,10344,10387,10387,14567,13992,15432)
case <- c(A,A,A,B,B,B,B,B,C,C)
year value case change
1989 10225 A 0.00
1990 10287 A 0.61 # ((100/10225)*10287)-100
1991 10262 A 0.36
1995 10087 B 0.00
1996 …Run Code Online (Sandbox Code Playgroud) r ×4
dataframe ×1
graph ×1
lattice ×1
matching ×1
mean ×1
missing-data ×1
multi-level ×1
plyr ×1
r-zelig ×1
regression ×1
time ×1