闪亮的可变零件配方
Variable parts formula for Shiny
在我的 Shiny 应用程序的这一部分,我将使用用户选择的变量进行 'linear model' (lm()
) 回归。共有三个输入:
input$lmTrendFun
是一个 selectInput()
,带有选项 c("Linear", "Exponential", "Logarithmic", "Quadratic", "Cubic")
:
selectInput("lmTrendFun", "Select the model for your trend line.",
choices = c("Linear", "Exponential", "Logarithmic", "Quadratic", "Cubic"))
第二个输入是input$lmDep
,也是selectInput()
。我首先在 observe()
反应函数中创建了一个 updateSelectInput
,因此选择的是导入的 tibble
.
中的列名
第三个输入是 input$lmInd
,它是一个 checkboxGroupInput()
,选择是除已经是 input$lmInd
的列名之外的所有列名。
由此我想要这个输出:这些变量的 lm()
(或者更确切地说,summary.lm()
或 summary(lm())
)结果。如果我知道它们是什么,那就简单了:
if(input$lmTrendFun == "Linear"){
form <- yname ~ x1 + x2
}else if(input$lmTrendFun == "Exponential"){
form <- yname~ exp(x1) + exp(x2)
}else if(input$lmTrendFun == "Logarithmic"){
form <- yname~ log(x1) + log(x2)
}else if(input$lmTrendFun == "Quadratic"){
form <- yname ~ poly(x1, 2) + poly(x2, 2)
}else if(input$lmTrendFun == "Cubic"){
form <- y ~ poly(x1, 3) + poly(x2, 3)
}
[...]
lm(form, data = .)
其中数据 (.
) 具有列 yname
、x1
和 x2
。
但是,我没有。所以我相信我需要一些可以创建公式的更通用的函数。如何做到这一点?
formulizer <- function() as.formula(paste0( input$lmDep, "~", switch(input$lmTrendFun,
Linear = paste0(input$lmInd, collapse=" + "),
Logarithmic = paste0("exp(", input$lmInd,")", collapse=" + "),
Quadratic = paste0("poly(", input$lmInd,", 2)", collapse=" + "),
Cubic = paste0("poly(", input$lmInd,", 3)", collapse=" + ") )))
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Linear", lmDep="Vp")
> formulaizer()
Vp ~ V1 + V2 + V3 + V4 + V5
<environment: 0x7fad1cf63d48>
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Logarithmic", lmDep="Vp")
> formulizer()
Vp ~ exp(V1) + exp(V2) + exp(V3) + exp(V4) + exp(V5)
<environment: 0x7fad01e694d0>
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Quadratic", lmDep="Vp")
> formulizer()
Vp ~ poly(V1, 2) + poly(V2, 2) + poly(V3, 2) + poly(V4, 2) +
poly(V5, 2)
<environment: 0x7fad01f51d20>
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Cubic", lmDep="Vp")
> formulizer()
Vp ~ poly(V1, 3) + poly(V2, 3) + poly(V3, 3) + poly(V4, 3) +
poly(V5, 3)
<environment: 0x7fad01f59690>
考虑 switch
和矢量化 paste0
来构建带有转换的项,然后将项传递给 reformulate
。将以下输入调整为实际的闪亮变量:
dep_term <- ...
ind_terms <- ...
form <- switch(input$lmTrendFun,
Linear = reformulate(ind_terms, response="yname"),
Exponential = reformulate(paste0("exp(", ind_terms, ")"), response=dep_term),
Logarithmic = reformulate(paste0("log(", ind_terms, ")"), response=dep_term),
Quadratic = reformulate(paste0("poly(", ind_terms, ", 2)"), response=dep_term),
Cubic = reformulate(paste0("poly(", ind_terms, ", 3)"), response=dep_term)
)
在我的 Shiny 应用程序的这一部分,我将使用用户选择的变量进行 'linear model' (lm()
) 回归。共有三个输入:
input$lmTrendFun
是一个selectInput()
,带有选项c("Linear", "Exponential", "Logarithmic", "Quadratic", "Cubic")
:selectInput("lmTrendFun", "Select the model for your trend line.", choices = c("Linear", "Exponential", "Logarithmic", "Quadratic", "Cubic"))
第二个输入是
中的列名input$lmDep
,也是selectInput()
。我首先在observe()
反应函数中创建了一个updateSelectInput
,因此选择的是导入的tibble
.第三个输入是
input$lmInd
,它是一个checkboxGroupInput()
,选择是除已经是input$lmInd
的列名之外的所有列名。
由此我想要这个输出:这些变量的 lm()
(或者更确切地说,summary.lm()
或 summary(lm())
)结果。如果我知道它们是什么,那就简单了:
if(input$lmTrendFun == "Linear"){
form <- yname ~ x1 + x2
}else if(input$lmTrendFun == "Exponential"){
form <- yname~ exp(x1) + exp(x2)
}else if(input$lmTrendFun == "Logarithmic"){
form <- yname~ log(x1) + log(x2)
}else if(input$lmTrendFun == "Quadratic"){
form <- yname ~ poly(x1, 2) + poly(x2, 2)
}else if(input$lmTrendFun == "Cubic"){
form <- y ~ poly(x1, 3) + poly(x2, 3)
}
[...]
lm(form, data = .)
其中数据 (.
) 具有列 yname
、x1
和 x2
。
但是,我没有。所以我相信我需要一些可以创建公式的更通用的函数。如何做到这一点?
formulizer <- function() as.formula(paste0( input$lmDep, "~", switch(input$lmTrendFun,
Linear = paste0(input$lmInd, collapse=" + "),
Logarithmic = paste0("exp(", input$lmInd,")", collapse=" + "),
Quadratic = paste0("poly(", input$lmInd,", 2)", collapse=" + "),
Cubic = paste0("poly(", input$lmInd,", 3)", collapse=" + ") )))
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Linear", lmDep="Vp")
> formulaizer()
Vp ~ V1 + V2 + V3 + V4 + V5
<environment: 0x7fad1cf63d48>
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Logarithmic", lmDep="Vp")
> formulizer()
Vp ~ exp(V1) + exp(V2) + exp(V3) + exp(V4) + exp(V5)
<environment: 0x7fad01e694d0>
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Quadratic", lmDep="Vp")
> formulizer()
Vp ~ poly(V1, 2) + poly(V2, 2) + poly(V3, 2) + poly(V4, 2) +
poly(V5, 2)
<environment: 0x7fad01f51d20>
> input <- list(lmInd=paste0("V", 1:5), lmTrendFun="Cubic", lmDep="Vp")
> formulizer()
Vp ~ poly(V1, 3) + poly(V2, 3) + poly(V3, 3) + poly(V4, 3) +
poly(V5, 3)
<environment: 0x7fad01f59690>
考虑 switch
和矢量化 paste0
来构建带有转换的项,然后将项传递给 reformulate
。将以下输入调整为实际的闪亮变量:
dep_term <- ...
ind_terms <- ...
form <- switch(input$lmTrendFun,
Linear = reformulate(ind_terms, response="yname"),
Exponential = reformulate(paste0("exp(", ind_terms, ")"), response=dep_term),
Logarithmic = reformulate(paste0("log(", ind_terms, ")"), response=dep_term),
Quadratic = reformulate(paste0("poly(", ind_terms, ", 2)"), response=dep_term),
Cubic = reformulate(paste0("poly(", ind_terms, ", 3)"), response=dep_term)
)