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

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

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

问题

I can provide the translated code for your request:

  1. 我正在使用MASS包中的波士顿房屋数据集。期望的目标是类似于这样的:
  2. ```R
  3. library(MASS)
  4. library(tidyverse)
  5. library(gam)
  6. Boston.splines <- gam(medv ~ s(crime) + s(zn) + s(indus), data = Boston)

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

  1. names_Boston <- names(Boston[,1:4])
  2. f1 <- paste("medv ~", paste(names_Boston, collapse = "+"))
  3. f1 <- as.formula(f1)
  4. Boston1.gam <- gam(f1, data = Boston)

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

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

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

返回了一个错误消息:

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

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

  1. Please note that the code itself remains in English as you requested, and only the surrounding text is translated.
  2. <details>
  3. <summary>英文:</summary>
  4. 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)

  1. 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)

  1. 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.
  2. I&#39;ve tried dplyr and base R, nothing works. For example, this:

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

  1. returned an error message:

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

  1. 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)?
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. 这对我有用:
  6. ``` r
  7. library(MASS)
  8. library(gam)
  9. #&gt; 加载所需的包: splines
  10. #&gt; 加载所需的包: foreach
  11. #&gt; 已加载 gam 1.22-2
  12. names_Boston &lt;- names(Boston[,1:3])
  13. f1 &lt;- as.formula(paste0(&#39;medv ~&#39;, paste0(&#39;s(&#39;, names_Boston, &#39;)&#39;, collapse = &#39;+&#39;)))
  14. gam(f1, data = Boston)
  15. #&gt; 调用:
  16. #&gt; gam(formula = f1, data = Boston)
  17. #&gt;
  18. #&gt; 自由度: 505 总数; 剩余 493.0002
  19. #&gt; 剩余偏差: 26249.62

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

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

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

This works for me:

  1. library(MASS)
  2. library(gam)
  3. #&gt; Loading required package: splines
  4. #&gt; Loading required package: foreach
  5. #&gt; Loaded gam 1.22-2
  6. names_Boston &lt;- names(Boston[,1:3])
  7. f1 &lt;- as.formula(paste0(&#39;medv ~&#39;, paste0(&#39;s(&#39;, names_Boston, &#39;)&#39;, collapse = &#39;+&#39;)))
  8. gam(f1, data = Boston)
  9. #&gt; Call:
  10. #&gt; gam(formula = f1, data = Boston)
  11. #&gt;
  12. #&gt; Degrees of Freedom: 505 total; 493.0002 Residual
  13. #&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:

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

答案2

得分: 1

你可以尝试:

  1. names_Boston <- names(Boston[,1:3])
  2. f1 <- paste("medv ~", paste0(sprintf("s(%s)", names_Boston), collapse = " + "))
  3. f1
  4. [1] "medv ~ s(crim) + s(zn) + s(indus)"
  5. gam(as.formula(f1), data = Boston)
  6. Call:
  7. gam(formula = as.formula(f1), data = Boston)
  8. Degrees of Freedom: 505 total; 493.0002 Residual
  9. Residual Deviance: 26249.62
  10. gam(medv ~ s(crim) + s(zn) + s(indus), data = Boston)
  11. Call:
  12. gam(formula = medv ~ s(crim) + s(zn) + s(indus), data = Boston)
  13. Degrees of Freedom: 505 total; 493.0002 Residual
  14. Residual Deviance: 26249.62
英文:

You may try

  1. names_Boston &lt;- names(Boston[,1:3])
  2. f1 &lt;- paste(&quot;medv ~&quot;, paste0(sprintf(&quot;s(%s)&quot;, names_Boston), collapse = &quot; + &quot;))
  3. f1
  4. [1] &quot;medv ~ s(crim) + s(zn) + s(indus)&quot;
  5. gam(as.formula(f1), data = Boston)
  6. Call:
  7. gam(formula = as.formula(f1), data = Boston)
  8. Degrees of Freedom: 505 total; 493.0002 Residual
  9. Residual Deviance: 26249.62
  10. gam(medv ~ s(crim) + s(zn) + s(indus), data = Boston)
  11. Call:
  12. gam(formula = medv ~ s(crim) + s(zn) + s(indus), data = Boston)
  13. Degrees of Freedom: 505 total; 493.0002 Residual
  14. 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:

确定