英文:
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'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't seem to get the s() function to be added to the front of each of the column names.
I'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)
#> 加载所需的包: splines
#> 加载所需的包: foreach
#> 已加载 gam 1.22-2
names_Boston <- names(Boston[,1:3])
f1 <- as.formula(paste0('medv ~', paste0('s(', names_Boston, ')', collapse = '+')))
gam(f1, data = Boston)
#> 调用:
#> gam(formula = f1, data = Boston)
#>
#> 自由度: 505 总数; 剩余 493.0002
#> 剩余偏差: 26249.62
<sup>创建于2023-06-01,使用 reprex v2.0.2</sup>
但第4列无法平滑处理,因为:
一个平滑变量遇到了3个或更少的唯一值;需要至少4个
英文:
This works for me:
library(MASS)
library(gam)
#> Loading required package: splines
#> Loading required package: foreach
#> Loaded gam 1.22-2
names_Boston <- names(Boston[,1:3])
f1 <- as.formula(paste0('medv ~', paste0('s(', names_Boston, ')', collapse = '+')))
gam(f1, data = Boston)
#> Call:
#> gam(formula = f1, data = Boston)
#>
#> Degrees of Freedom: 505 total; 493.0002 Residual
#> 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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论