我对R中的面板数据有疑问。
我的数据基本上是这样的:
Year Name Variable Treatment
2000 CompanyA 10 0
2001 CompanyA 10 0
2002 CompanyA 10 1
2003 CompanyA 10 0
2004 CompanyA 12 0
2005 CompanyA 12 0
1999 CompanyB 5 1
2000 CompanyB 5 1
2001 CompanyB 5 0
2002 CompanyB 5 0
2003 CompanyB 6 0
2004 CompanyB 5 0
2005 CompanyB 6 0
2006 CompanyB 6 0
有没有机会计算R中治疗前后因变量的差异(关于一定的时滞)?
不幸的是,我只有不平衡的面板数据。计算的目的是从中制作一个虚拟变量。这将显示因变量是否在两年后增长。然后,我想对它进行clogit回归。
我需要知道治疗后因变量是否发生了变化。 所以我需要某种代码来计算关于我的变量的每一个积极变化的假人。
输出应该是这样的:
Year Name Variable Treatment Dummy
2000 CompanyA 10 0 0
2001 CompanyA 10 0 0
2002 CompanyA 10 1 0
2003 CompanyA 10 0 0
2004 CompanyA 12 0 1
2005 CompanyA 12 0 1
1999 CompanyB 5 1 0
2000 CompanyB 5 1 0
2001 CompanyB 5 0 0
2002 CompanyB 5 0 0
2003 CompanyB 6 0 1
2004 CompanyB 5 0 0
2005 CompanyB 6 0 0
2006 CompanyB 6 0 0
所以我可以对其进行条件logit回归并将处理(包括其他变量)链接到一定时滞后对我的因变量的正面影响。
答案 0 :(得分:1)
根据评论中的说明更新了答案;除了简单的比较(开/关处理,A部分)之外,我根据要求采用了时间进程的方法(B部分) 请注意,在许多方面,代码需要根据确切的问题进行调整(如何处理成为治疗的人,然后甚至可能再次进行治疗?从开始(或停止后)预测治疗效果的持续时间是多长? o ftretment?这些问题可能更像概念而不是R问题,但我试图提供一些起点如何实现这些问题。
#### sample data (added and changed some data to demonstarte sorting of the years ####
# and pos Treatment at first time point):
text <- "Year Name Variable Treatment
2000 CompanyA 10 0
2001 CompanyA 10 0
2002 CompanyA 10 1
2003 CompanyA 10 0
2004 CompanyA 12 0
2010 CompanyA 15 1
2005 CompanyA 12 0
1999 CompanyB 5 0
2000 CompanyB 5 1
2001 CompanyB 5 0
2002 CompanyB 5 0
2003 CompanyB 6 0
2004 CompanyB 5 0
2005 CompanyB 6 0
2006 CompanyB 6 0
2001 CompanyC 5 1
2006 CompanyC 9 1"
df <- read.table(text=text, header=TRUE)
str(df)
head(df)
#### A) Simple way: just compare on/off treatment subject ####
mean(df[df$Treatment==1, "Variable"]) - mean(df[df$Treatment==0, "Variable"])
#### B) Compare within each company, take into consideration also the time course ####
# split to list according to company names, to analyse them separately
Name.u <- as.character(unique(df$Name)) # unique Company names
L <- sapply(Name.u, function(n) df[df$Name==n, ], simplify=FALSE)
str(L)
L # a list of dataframes, one dataframe for each company
## deal with special cases that may influence the concept of theanalysis
# sort for year (assuming there are nor ties)
L <- sapply(Name.u, function(n) L[[n]][order(L[[n]]$Year), ], simplify=FALSE)
# posibly ignore those who were already treatet at study entry already
L.del <- sapply(Name.u, function(n) ifelse(L[[n]][1, "Treatment"]==1, TRUE, FALSE), simplify=TRUE)
L[L.del] <- NULL
Name.u <- Name.u[!L.del]
str(L); L # note that CompanyC was deleted because of Treatment==1 at start
## display treatment duration etc.
LL <- function(L.n) {
L.n$diff <- c(0, diff(L.n$Treatment))
# stopifnot(sum(L.n$diff!=0) == 1) # more than one status change - need clarification how this should be handled, see also lines below
# ALL status change to "treated" (possibly more than one!)
Rx.start <- which(L.n$diff==1)
# duration since FIRST documented treatment
L.n$RxDurSinceFirst <- L.n$Year - min(L.n$Year[Rx.start])
L.n$RxDurReal <- L.n$RxDur
# need to define what to do with those who are Treatment negative at THIS time ...
L.n$RxDurReal[L.n$Treatment==0] <- NA
# ... and those who became Treatment neg before or now
L.n$RxDurReal[sapply(1:nrow(L.n), function(row.i) row.i >= min(which(L.n$diff==-1)))] <- NA
return(L.n)
}
str(LL)
# L2 is a new list of the same structure as L, but with more information
# (more columns in each dataframe element)
L2 <- sapply(Name.u, function(n) LL(L[[n]]), simplify=FALSE)
str(L2)
L2
# for a company n one can then do (and of course further vectorize):
n <- Name.u[1]
str(L2[[n]])
L2[[n]]
# for a company n one can then compare RxDurSinceFirst, RxDurReal or
# whateveryou want (and of course further vectorize):
(Var.before <- L2[[n]]$Variable[ L2[[n]]$RxDurSinceFirst < 0 ] )
(Var.after <- L2[[n]]$Variable[ L2[[n]]$RxDurSinceFirst >= 0 ] )
t.test(Var.before, Var.after) # works of course only if enough observations
# or on/off Treatment within one group, and use the means of each group
# for further paired t.test/ U-test etc.
(Var.OnRx <- L2[[n]]$Variable[ L2[[n]]$Treatment == 0 ] )
(Var.OffRx <- L2[[n]]$Variable[ L2[[n]]$Treatment == 1 ] )
### End ###
答案 1 :(得分:1)
或者,
diff(by(df$Variable, df$Treatment, FUN=mean))
#[1] -1.242424
答案 2 :(得分:0)
这是一个我认为会让你非常接近的答案。我的代码突出显示了治疗前变量的任何变化。请注意,这不是最优雅的代码,或多或少是草稿版本,但我必须打包,我认为这可能仍然有用。
首先,这是你的桌子的输入。只需运行它来加载表格。
dfx <- structure(list(Year = c(2000L, 2001L, 2002L, 2003L, 2004L, 2005L,
1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L), Name = c("CompanyA",
"CompanyA", "CompanyA", "CompanyA", "CompanyA", "CompanyA", "CompanyB",
"CompanyB", "CompanyB", "CompanyB", "CompanyB", "CompanyB", "CompanyB",
"CompanyB"), Variable = c(10L, 10L, 10L, 10L, 12L, 12L, 5L, 5L,
5L, 5L, 6L, 5L, 6L, 6L), Treatment = c(0L, 0L, 1L, 0L, 0L, 0L,
1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), Dummy = c(0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L)), .Names = c("Year", "Name",
"Variable", "Treatment", "Dummy"), class = "data.frame", row.names = c(NA,
-14L))
然后我创建了一个辅助变量(has_treatment),表明某个年份(行)是否有治疗。这是此函数中的前两行。
然后是一个简单的条件陈述,在这个陈述中,我测试一个案例是否有治疗,如果变量在治疗前变量与变量不同。
foo <- function(dfx){
dfx[(Position( isTRUE, diff(dfx$Treatment) == -1)+1) : nrow(dfx), "has_treatment" ] <- 1
dfx[1:(Position( isTRUE, diff(dfx$Treatment) == -1)) , "has_treatment" ] <- 0
dfx[dfx$has_treatment == 1 &
((dfx[dfx$Treatment == 1, "Variable"] ==
dfx[, "Variable"])==FALSE) ,"dummy"] <- 1
return(dfx)
}
然后我在ddply中运行它。如果您不熟悉ddply和plyr软件包,我强烈建议您学习它。
library(plyr)
ddply(test, .variables = "Name", foo )
同样,这不是你想要的,但原则上它应该让你走上正轨。我会试着给它另一个镜头,但我必须跑。
此外,正如有些人可能评论这不是最优雅的方式,并且可能有更快更有效的方法。
无论如何,我希望它有所帮助。