如何将函数应用于多个数据帧

问题描述 投票:0回答:1

我想写一个函数,在一个数据框架中使用 roll_lm 包中的函数 roll. 然后,我想将此函数应用于具有相同格式,但不同x值的多个数据帧。我正在运行一个回归,x=RunTime,其余列是单独的y值。RunTime的值对于每个数据帧都是不同的。下面是我到目前为止在数据框Sum_9.18上运行所需函数的情况,其中我有许多其他类似的数据框。

fun1 <- function(y) {
roll_lm(Sum_9.18$RunTime, y, width = 100)
}
test1 <- data.frame(lapply(Sum_9.18[2:11], fun1))
r function apply
1个回答
0
投票

我将给出一个解决方案,使用 lm() 模型,因为我没有 roll 包的安装。

首先,我将假设你能够将数据帧存储在一个列表中。在这种情况下,你可以使用 lapply() 迭代您要应用您的 fun1() 函数。

所以,从概念上讲,我们会有两个不同的函数,分别由 lapply(), 一边 外层 迭代的 数据帧一边 内层 迭代的 专栏 的分析数据框架。内部函数是您的 fun1() 函数,但我将把它称为 fun_inner() 以提高清晰度。

所以,假设你有一个数据帧列表,称为 list_of_dfs那么下面的函数将允许你做你所需要的事情(你只需要修改一下下面的 fun_inner() 函数来调用 roll_lm() 函数)。)

#' Fits a predictive model using each column of a data frame as target variable
#'
#' @param df data frame with the data to fit.
#' @param target_columns name or index of the column(s) in data frame \code{df}
#' to be used as target variables, one at a time.
#' @param pred_columns name or index of the column(s) in data frame \code{df}
#' to be used as predictor variables.
#' @param fun_model function to be called that fits the model \code{y ~ X}
#' where \code{y} is each variable taken from the \code{target_columns} of the input data frame
#' and \code{X} is the set of predictor variables taken from the \code{pred_columns}
#' of the input data frame.
#' 
#' @return a list containing the result of the model fit to each target variable.
fun_outer_iterate_on_dfs <- function(df, target_columns, pred_columns, fun_model) {
  lapply(df[, target_columns, drop=FALSE], fun_model, as.matrix(df[, pred_columns, drop=FALSE]))
}

#' Fits an lm( y ~ X ) model. \code{y} is a numeric vector and \code{X} is a matrix.
fun_inner_fit_model <- function(y, X) {
  lm( y ~ X )
}

这是一个用例。

set.seed(1717)
nobs = 10
# List containing the data frames to be used in the model fits
list_of_dfs[[1]] = data.frame(RunTime=rnorm(nobs), y1=rnorm(nobs), y2=rnorm(nobs))
list_of_dfs[[2]] = data.frame(RunTime=rnorm(nobs), y1=rnorm(nobs), y2=rnorm(nobs))
list_of_dfs[[3]] = data.frame(RunTime=rnorm(nobs), y1=rnorm(nobs), y2=rnorm(nobs))

test_models_on_each_df <- lapply(list_of_dfs, fun_outer_iterate_on_dfs, c("y1", "y2"),
"RunTime", fun_inner_fit_model)

请注意,我们如何能将更多的参数(除了第一个参数)传递给由 lapply() 只需将它们列在函数名称之后(本例中列在 fun_outer_iterate_on_dfs).

传递给 fun_outer_iterate_on_dfs() 功能也可以是 列数.

上面的代码给出了这样的内容。

[[1]]
[[1]]$y1

Call:
lm(formula = y ~ X)

Coefficients:
(Intercept)            X  
   -0.05994     -0.11727  

[[1]]$y2

Call:
lm(formula = y ~ X)

Coefficients:
(Intercept)            X  
    0.02854     -0.08574  

[[2]]
[[2]]$y1

Call:
lm(formula = y ~ X)

Coefficients:
(Intercept)            X  
   -0.23479     -0.01973  

[[2]]$y2

Call:
lm(formula = y ~ X)

Coefficients:
(Intercept)            X  
    0.07248     -0.33088  

[[3]]
[[3]]$y1

Call:
lm(formula = y ~ X)

Coefficients:
(Intercept)            X  
    -0.3087      -0.1191  

[[3]]$y2

Call:
lm(formula = y ~ X)

Coefficients:
(Intercept)            X  
     0.1765       0.5085  

我们可以看到,三个数据帧中的每一个都有两个回归拟合,一个是对目标变量的拟合 y1 和一个目标变量 y2.


最后,如果你已经将数据帧存储为不同的对象,你可以使用下面的方法。在列表中存储数据帧的代码片段,它假定所有的数据帧名称都遵循以下模式 Sum_* 并且在工作空间中定义的所有具有这种模式的对象都是感兴趣的数据帧。

#' Stores objects in the workspace whose name satisfies a given pattern in a list
#' 
#' @param pattern regular expression to be satisfied by the name of the object to store in the list.
#' @return a named list whose elements are the objects found in the parent environment satisfying the given pattern.
#' The name of each element is the name of the object.
store_objects_in_list <- function(pattern) {
  object_names = ls(pattern=pattern, envir=parent.frame())
  list_with_objects = lapply(object_names, get)
  names(list_with_objects) = object_names
  return(list_with_objects)
}
list_of_dfs <- store_objects_in_list("^Sum_")
© www.soinside.com 2019 - 2024. All rights reserved.