平滑样条,是否有一种方法可以自动将s()添加到每个列名称中?

huangapple go评论69阅读模式
英文:

Smoothing splines, is there a way to automatically add s() to each column name?

问题

I can provide the translated code for your request:

我正在使用MASS包中的波士顿房屋数据集。期望的目标是类似于这样的:

```R
library(MASS)
library(tidyverse)
library(gam)
Boston.splines <- gam(medv ~ s(crime) + s(zn) + s(indus), data = Boston)

我可以自动完成除了样条函数部分之外的所有内容:

names_Boston <- names(Boston[,1:4])

f1 <- paste("medv ~", paste(names_Boston, collapse = "+"))
f1 <- as.formula(f1)
Boston1.gam <- gam(f1, data = Boston)

但是,我无论如何都似乎无法在每个列名的前面添加s()函数。

我尝试过dplyr和基本的R,但都不起作用。例如,这个:

set_names(paste0('s(', paste0(names_Boston), paste0(')')))

返回了一个错误消息:

Error: unexpected string constant in "set_names(paste0('s(', paste0(names_Boston), paste0(')')))"

有什么方法可以自动将平滑样条函数添加到列名以得到类似于gam(medv ~ s(crime) + s(zn) + s(indus), data = Boston)的公式吗?


Please note that the code itself remains in English as you requested, and only the surrounding text is translated.

<details>
<summary>英文:</summary>

I&#39;m working with the Boston Housing data set in the MASS package. The desired goal is something like this:

library(MASS)
library(tidyverse)
library(gam)
Boston.splines <- gam(medv ~ s(crime) + s(zn) + s(indus), data = Boston)


I can get everything except the spline function to work automatically:

names_Boston <- names(Boston[,1:4])

f1 <- paste("medv ~", paste(names_Boston, collapse = "+"))
f1 <- as.formula(f1)
Boston1.gam <- gam(f1, data = Boston)

But for the life of me I can&#39;t seem to get the s() function to be added to the front of each of the column names.

I&#39;ve tried dplyr and base R, nothing works. For example, this:

set_names(paste0('s(', paste0(names_Boston), paste0')))


returned an error message:

Error: unexpected string constant in "set_names(paste0('s(', paste0(names_Boston), paste0')'"


What is a way to automatically add the smoothing spline function to column names to result in a formula such as gam(medv ~ s(crime) + s(zn) + s(indus), data = Boston)?

</details>


# 答案1
**得分**: 2

这对我有用:

``` r
library(MASS)
library(gam)
#&gt; 加载所需的包: splines
#&gt; 加载所需的包: foreach
#&gt; 已加载 gam 1.22-2
names_Boston &lt;- names(Boston[,1:3])
f1 &lt;- as.formula(paste0(&#39;medv ~&#39;, paste0(&#39;s(&#39;, names_Boston, &#39;)&#39;, collapse = &#39;+&#39;)))
gam(f1, data = Boston)
#&gt; 调用:
#&gt; gam(formula = f1, data = Boston)
#&gt; 
#&gt; 自由度: 505 总数; 剩余 493.0002
#&gt; 剩余偏差: 26249.62

<sup>创建于2023-06-01,使用 reprex v2.0.2</sup>

但第4列无法平滑处理,因为:

一个平滑变量遇到了3个或更少的唯一值;需要至少4个
英文:

This works for me:

library(MASS)
library(gam)
#&gt; Loading required package: splines
#&gt; Loading required package: foreach
#&gt; Loaded gam 1.22-2
names_Boston &lt;- names(Boston[,1:3])
f1 &lt;- as.formula(paste0(&#39;medv ~&#39;, paste0(&#39;s(&#39;, names_Boston, &#39;)&#39;, collapse = &#39;+&#39;)))
gam(f1, data = Boston)
#&gt; Call:
#&gt; gam(formula = f1, data = Boston)
#&gt; 
#&gt; Degrees of Freedom: 505 total; 493.0002 Residual
#&gt; Residual Deviance: 26249.62

<sup>Created on 2023-06-01 with reprex v2.0.2</sup>

But column 4 cannot be smoothed due to:

A smoothing variable encountered with 3 or less unique values; at least 4 needed

答案2

得分: 1

你可以尝试:

names_Boston <- names(Boston[,1:3])
f1 <- paste("medv ~", paste0(sprintf("s(%s)", names_Boston), collapse = " + "))

f1
[1] "medv ~ s(crim) + s(zn) + s(indus)"

gam(as.formula(f1), data = Boston)

Call:
gam(formula = as.formula(f1), data = Boston)

Degrees of Freedom: 505 total; 493.0002 Residual
Residual Deviance: 26249.62

gam(medv ~ s(crim) + s(zn) + s(indus), data = Boston)

Call:
gam(formula = medv ~ s(crim) + s(zn) + s(indus), data = Boston)

Degrees of Freedom: 505 total; 493.0002 Residual
Residual Deviance: 26249.62
英文:

You may try

names_Boston &lt;- names(Boston[,1:3])
f1 &lt;- paste(&quot;medv ~&quot;, paste0(sprintf(&quot;s(%s)&quot;, names_Boston), collapse = &quot; + &quot;))

f1
[1] &quot;medv ~ s(crim) + s(zn) + s(indus)&quot;


gam(as.formula(f1), data = Boston)

Call:
gam(formula = as.formula(f1), data = Boston)

Degrees of Freedom: 505 total; 493.0002 Residual
Residual Deviance: 26249.62 

gam(medv ~ s(crim) + s(zn) + s(indus), data = Boston)

Call:
gam(formula = medv ~ s(crim) + s(zn) + s(indus), data = Boston)

Degrees of Freedom: 505 total; 493.0002 Residual
Residual Deviance: 26249.62 

huangapple
  • 本文由 发表于 2023年6月2日 08:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定