Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑并重用R中模型调用的公式部分_R - Fatal编程技术网

编辑并重用R中模型调用的公式部分

编辑并重用R中模型调用的公式部分,r,R,我在R中安装了一个神经网络: model = nnet(f1/max(f1)~n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12,data=md,size=3) 从模型对象中,我可以提取调用的公式,model$call$formula,它返回类型为call的对象,并显示为长度为3的列表。我知道我可以在nnet()调用中的call对象上使用as.formula(),但我不知道如何首先编辑它 我要做的是修改公式,然后将其传递回nnet()。具体而言,我希望能够更改因变量并

我在R中安装了一个神经网络:

model = nnet(f1/max(f1)~n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12,data=md,size=3)
从模型对象中,我可以提取调用的公式,
model$call$formula
,它返回类型为
call
的对象,并显示为长度为3的列表。我知道我可以在
nnet()
调用中的
call
对象上使用
as.formula()
,但我不知道如何首先编辑它

我要做的是修改公式,然后将其传递回
nnet()
。具体而言,我希望能够更改因变量并添加或删除因变量。

您需要使用:

update.formula用于更新模型公式。这通常涉及添加或删除术语,但更新可能更一般

以下是使用mtcars中的变量可以查看的一些示例:

f1 <- mpg ~ cyl + wt
f2 <- update(f1, . ~ . + am)
f2
## mpg ~ cyl + wt + am
f3 <- update(f2, hp ~ .)
f3
## hp ~ cyl + wt + am
f4 <- update(f3, mpg ~ hp)
f4
## mpg ~ hp
f1您需要使用:

update.formula用于更新模型公式。这通常涉及添加或删除术语,但更新可能更一般

以下是使用mtcars中的变量可以查看的一些示例:

f1 <- mpg ~ cyl + wt
f2 <- update(f1, . ~ . + am)
f2
## mpg ~ cyl + wt + am
f3 <- update(f2, hp ~ .)
f3
## hp ~ cyl + wt + am
f4 <- update(f3, mpg ~ hp)
f4
## mpg ~ hp
f1您也可以“手动”完成,而无需
update.formula
。 我想使用
update.formula
可能更容易,但我还是给你另一种方法:

## your formula
f.example <- x ~ n1 + n2
## you can extract each member of the formula with [[2/1/3]] 
f.left.side <-as.character(f.example[[2]])  # "x"
f.middle <- as.character(f.example[[1]])  # "~"
f.right.side <- as.character(f.example[[3]])  # "n1 + n2"
## you do your change, e.g:
f.right.side.new <- "n1 + n3"
## you rebuild your formula, first as a character (%s for string), then as a formula
f.new <- sprintf("%s ~ %s", f.left.side, f.right.side.new)
f.new <- as.formula(f.new)
##您的公式
f、 示例您也可以“手动”执行,而无需
update.formula
。 我想使用
update.formula
可能更容易,但我还是给你另一种方法:

## your formula
f.example <- x ~ n1 + n2
## you can extract each member of the formula with [[2/1/3]] 
f.left.side <-as.character(f.example[[2]])  # "x"
f.middle <- as.character(f.example[[1]])  # "~"
f.right.side <- as.character(f.example[[3]])  # "n1 + n2"
## you do your change, e.g:
f.right.side.new <- "n1 + n3"
## you rebuild your formula, first as a character (%s for string), then as a formula
f.new <- sprintf("%s ~ %s", f.left.side, f.right.side.new)
f.new <- as.formula(f.new)
##您的公式
f、 范例