【问题标题】:Adding descriptive statistics to a function when making use of the ellipsis as input variable使用省略号作为输入变量时向函数添加描述性统计
【发布时间】:2021-02-05 20:10:39
【问题描述】:

对于一个作业,我在 R 中创建了一个函数,用于计算对多元线性回归有用的数据的回归系数、预测值和残差。它是这样做的:

MLR <- function(y_var, ...){  
  
  y <- y_var  
  X <- as.matrix(cbind(...))  
  
  intercept <- rep(1, length(y)) 
  
  X <- cbind(intercept, X) 
  
  regression_coef <- solve(t(X) %*% X) %*% t(X) %*% y  
  
  predicted_val <- X %*% regression_coef 
  
  residual_val <- y - predicted_val 
 
  
  scatterplot <- plot(predicted_val, residual_val,
                      ylab = 'Residuals', xlab = 'Predicted values',
                      main = 'Predicted values against the residuals',
                      abline(0,0))
 
  list('y' = y, 
       'X' = X, 
       'Regression coefficients' = regression_coef,
       'Predicted values' = predicted_val, 
       'Residuals' = residual_val,
       'Scatterplot' = scatterplot
       )
}

现在,我的努力是为我的输入变量添加描述性统计数据。因为我希望我的自变量可以是任意数字,所以我使用省略号作为输入变量。有没有办法计算我的自变量(由...定义)的有用描述性统计数据(均值、方差、标准差)?

这个

mean(...)

没有用...

已经感谢您的回复!

【问题讨论】:

    标签: r function regression


    【解决方案1】:

    尝试对您的功能进行这种细微的更改。我已应用于iris 数据集的一些变量。您可以通过 X 计算所需的统计信息,然后将其作为输出的附加槽输出。代码如下:

    #Function
    MLR <- function(y_var, ...){  
      
      y <- y_var
      X <- as.matrix(cbind(...))  
      RX <- X
      
      intercept <- rep(1, length(y)) 
      
      X <- cbind(intercept, X) 
      
      regression_coef <- solve(t(X) %*% X) %*% t(X) %*% y  
      
      predicted_val <- X %*% regression_coef 
      
      residual_val <- y - predicted_val 
      
      
      scatterplot <- plot(predicted_val, residual_val,
                          ylab = 'Residuals', xlab = 'Predicted values',
                          main = 'Predicted values against the residuals',
                          abline(0,0))
      
      #Summary
      #Stats
      DMeans <- apply(RX,2,mean,na.rm=T)
      DSD <- apply(RX,2,sd,na.rm=T)
      DVar <- apply(RX,2,var,na.rm=T)
      DSummary <- rbind(DMeans,DSD,DVar)
      #Out
      list('y' = y, 
           'X' = X, 
           'Regression coefficients' = regression_coef,
           'Predicted values' = predicted_val, 
           'Residuals' = residual_val,
           'Scatterplot' = scatterplot,
           'Summary' = DSummary
      )
    }
    #Apply
    MLR(y_var = iris$Sepal.Length,iris$Sepal.Width,iris$Petal.Length)
    

    输出的最终槽将如下所示:

    $Scatterplot
    NULL
    
    $Summary
                [,1]     [,2]
    DMeans 3.0573333 3.758000
    DSD    0.4358663 1.765298
    DVar   0.1899794 3.116278
    

    【讨论】:

      【解决方案2】:

      我想我明白了。不幸的是,省略号似乎与他们一起工作很奇怪。检查 cbind(...) 在你的函数中是否正确运行(当我在输出中检查它时,它只有 1 列宽,而我在其中输入了 2 个变量,这似乎不正确。

      我的解决方案不读取变量名 - 它使用占位符名称(Var_1、Var_2、...、Var_n)

      
      MLR <- function(y_var, ...){  
        
        # these two packages will come in handy
        
        require(dplyr)
        require(tidyr)
        
        y <- y_var  
        X <- as.matrix(cbind(...))
        
        # firstly, we need to make df/tibble out of ellipsis
        
        X2 <- list(...)
        
        n <- tibble(n = rep(0, times = length(y)))
        
        index <- 0
        
        for(Var in X2){
          
          index <- index + 1
          n[, paste0("Var_", index)] <- Var
          
        }
        
        # after the df was created, now it's time for calculating desc
        # Using tidyr::gather with dplyr::summarize creates nice summary, 
        # where each row is another variable
        
        descriptives <- tidyr::gather(n, key = "Variable", value = "Value") %>%
          group_by(Variable) %>%
          summarize(mean = mean(Value), var = var(Value), sd = sd(Value), .groups = "keep")
        
        # everything except the output list is the same
        
        intercept <- rep(1, length(y)) 
        
        X <- cbind(intercept, X) 
        
        regression_coef <- solve(t(X) %*% X) %*% t(X) %*% y  
        
        predicted_val <- X %*% regression_coef 
        
        residual_val <- y - predicted_val 
        
        
        scatterplot <- plot(predicted_val, residual_val,
                            ylab = 'Residuals', xlab = 'Predicted values',
                            main = 'Predicted values against the residuals',
                            abline(0,0))
        
        
        list('y' = y, 
             'X' = X, 
             'Regression coefficients' = regression_coef,
             'Predicted values' = predicted_val, 
             'Residuals' = residual_val,
             'Scatterplot' = scatterplot,
             'descriptives' = descriptives[-1,] # need to remove the first row 
                                                # because it is "n" placeholder
        )
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-06
        相关资源
        最近更新 更多