首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有列表的R中函数的输出(lda模型、ggplot、dataframe)

带有列表的R中函数的输出(lda模型、ggplot、dataframe)
EN

Stack Overflow用户
提问于 2022-11-21 11:07:22
回答 1查看 17关注 0票数 0

我正在为如何在R中获得一个函数的输出而挣扎,我正在创建一个元素列表。显然这很简单,但是,我不能谈这件事。我检查了不同的问题,但没有结果。这可能是一个很简单的问题,而且可能是我错印了一行。

我正在创建的函数的输出有三个元素,我认为它们应该包括在一个列表中:

  • 线性判别分析模型(
  • A dataframe
  • A ggplot

)

这是我正在使用的函数的版本:

代码语言:javascript
复制
fun <- function(data, tooth_type = c("m1", "m2", "m3"), position = c("U", "L")) {
  model <- lda(Species ~ MD + BL, data = data) 
  # extract the information of the tooth we are working from the Omo database
  tooth <- omo_numeric_mdbl[omo_numeric_mdbl$Tooth == tooth_type & omo_numeric_mdbl$Position == position, ]
  tooth <- tooth[complete.cases(tooth[, 31:32]), ]
  predictions <- model %>%
    predict(tooth)
  pred <- as.data.frame(predictions$posterior)*100
  # plot
  lda.data <- cbind(data, predict(model)$x)
  ggplot_fun <- ggplot(lda.data, aes(LD1, LD2)) +
    geom_point(aes(color = Species))
  list(model, pred, ggplot_fun)
}

如您所见,函数中有三个对象我想存储:

  • pred:,这是dataframe
  • model:,这是lda information
  • ggplot_fun:,这是ggplot图像

我试过:

  • as.list(model, pred, ggplot_fun) -> unsuccessful
  • list(model, pred, ggplot_fun) -> -> unsuccessful
  • x <- list(model, pred, ggplot_fun); return(x) -> unsuccessful

最后,我希望的是:

代码语言:javascript
复制
x <- fun(data, "m3", "L") # for example

x$pred       # show the dataframe (and display as kable, if required)
x$model      # show the lda model, and to get all the details inside as x$model$posterior
x$ggplot_fun # display the ggplot function
EN

回答 1

Stack Overflow用户

发布于 2022-11-21 11:29:11

我终于解决了这个问题。为了用同样的问题回答其他人,我修改了示例的最后一行:

代码语言:javascript
复制
list(model, pred, ggplot_fun)

由这一个

代码语言:javascript
复制
list(LDA = model, Prediction = pred, Ggplot = ggplot_fun)

基本上,我把每个元素命名为。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74517965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档