我目前在差异模型方面存在差异,并希望以有效的方式衡量同一模型的不同指标。
例如,我有一个数据框,其中包含Miles Driven,Work Hoursed,State,Group,Time的列。
目前,我可以使用代码复制并粘贴每个指标的模型:
# Create DID models
model_miles <- lm(df$miles_driven ~ df$Group
+ df$Time
+ df$Group * df$Time, data = df)
model_hours <- lm(df$hours_worked ~ df$Group
+ df$Time
+ df$Group * df$Time, data = df)
# Select results using clustered standard errors. The purpose of this is to
# avoid making distributional assumptions about the errors in the models.
results_miles <- clubSandwich::coef_test(model_miles,
vcov = "CR2",
cluster = df$state,
test = "Satterthwaite")
results_hours <- clubSandwich::coef_test(model_hours,
vcov = "CR2",
cluster = df$state,
test = "Satterthwaite")
results <- data.table::rbindlist(list(results_miles, results_hours))
View(results)
我想以某种方式创建我的度量标准名称列表,并使用用户定义的函数循环遍历此列表,以便使此过程更快,更自动化,但我无法使其工作正确地:
#list of metrics
metrics <- c("miles_driven", "hours_worked")
udf <- function(metric, dataframe){
# Create DID model
model <- lm(dataframe$metric ~ df$Group
+ dataframe$Time
+ dataframe$Group * df$Time, data = dataframe)
# Select results using clustered standard errors. The purpose of this
is to
# avoid making distributional assumptions about the errors in the
models.
results_miles <- clubSandwich::coef_test(model_miles,
vcov = "CR2",
cluster = dataframe$state,
test = "Satterthwaite")[4,]
View(results)
}
lapply(metrics, udf)
任何见解都将受到赞赏。谢谢!
答案 0 :(得分:1)
如果您使用[[
代替$
,则会有效。
例如:
dat <- data.frame(A=1:1, B=1:2)
x <- "A"
lm(dat[[x]] ~ dat$B)
您可以使用get
和assign
在循环中执行此类更高级的操作。
答案 1 :(得分:0)
在这些情况下,我始终lm
在lm
中data
一个公式。以下示例。如果您提供dataframe$varname
library(glue)
dostuff <- function(metric, dataframe){
model.form <- '{metric} ~ b + c + c*d'
lm(as.formula(glue(model.form)), data = dataframe)
}
df <- data.frame(a = runif(10), b = runif(10), c = runif(10), d = runif(10))
dostuff('a', df)
参数,则可以只提供变量名称,而不是显式传递glue('{metric} ~ b + c + c*d')
paste(metric, '~ b + c + c*d')
。
a = [1,2,3]
# for loop begins
a[0] = a[0] # a is now [1,2,3]
print(a[0]) # prints 1
a[0] = a[1] # a is now [2,2,3]
print(a[0]) # prints 2
a[0] = a[2] # a is now [3,2,3]
print(a[0]) # prints 3
# for loop ends
print(a) # prints [3,2,3]
如果你想坚持基础R,下面的两个对象是相等的
entityManager.getTransaction().begin();
CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
Root<Profile> fromRoot = criteriaQuery.from(Profile.class);
criteriaQuery.select(fromRoot);
criteriaQuery.where(criteriaBuilder.equal(fromRoot.get("userName"), username),
criteriaBuilder.equal(fromRoot.get("password"), password));
List<Object> resultList = entityManager.createQuery(criteriaQuery).getResultList();
Profile dbProfile = null;
if (resultList.isEmpty()) {
// Handle Error
} else {
dbProfile = (Profile) resultList.get(0);
}
entityManager.getTransaction().commit();