如何在 R 中保存拟合模型?
How do I save fitted models in R?
我安装了回归树。我尝试使用 save() 函数保存拟合模型,但如果我关闭 R,加载对象并调用它,我会得到不同的输出。
代码如下:
training.set=iris[,-5]
library(tree)
set.seed(123)
part1 = sample(1:nrow(training.set), round(nrow(training.set)/2))
part2 = setdiff(1:nrow(training.set), part1)
tree.output = tree("Sepal.Length~.", data=training.set[part1,],
control=tree.control(nobs=length(part1), minsize=2, mindev=0.001))
prune.t = prune.tree(tree.output, newdata=training.set[part2,])
plot(prune.t)
J = prune.t$size[prune.t$dev == min(prune.t$dev)]
J
m.tree = prune.tree(tree.output,best=J)
save(m.tree,file="my model.RData")
m.tree
输出:
node), split, n, deviance, yval
* denotes terminal node
1) root 75 49.54000 5.756
2) Petal.Length < 4.3 41 6.69800 5.161
4) Petal.Length < 3.55 31 3.82800 5.019
8) Sepal.Width < 3.45 17 1.04900 4.794
16) Petal.Length < 1.45 9 0.52220 4.644 *
17) Petal.Length > 1.45 8 0.09875 4.962 *
9) Sepal.Width > 3.45 14 0.86930 5.293
18) Sepal.Width < 3.85 8 0.17880 5.138 *
19) Sepal.Width > 3.85 6 0.24000 5.500 *
5) Petal.Length > 3.55 10 0.32000 5.600 *
3) Petal.Length > 4.3 34 10.83000 6.474
6) Petal.Length < 5.7 28 3.46400 6.264
12) Petal.Width < 2.05 23 2.42600 6.187 *
13) Petal.Width > 2.05 5 0.26800 6.620 *
7) Petal.Length > 5.7 6 0.41500 7.450
14) Petal.Length < 6.35 4 0.02750 7.275 *
15) Petal.Length > 6.35 2 0.02000 7.800 *
脚本的第二部分:
q()
rm(list=ls() )
load("my model.RData")
m.tree
输出:
$frame
var n dev yval splits.cutleft splits.cutright
1 Petal.Length 75 49.5448000 5.756000 <4.3 >4.3
2 Petal.Length 41 6.6975610 5.160976 <3.55 >3.55
4 Sepal.Width 31 3.8283871 5.019355 <3.45 >3.45
8 Petal.Length 17 1.0494118 4.794118 <1.45 >1.45
16 <leaf> 9 0.5222222 4.644444
17 <leaf> 8 0.0987500 4.962500
9 Sepal.Width 14 0.8692857 5.292857 <3.85 >3.85
18 <leaf> 8 0.1787500 5.137500
19 <leaf> 6 0.2400000 5.500000
5 <leaf> 10 0.3200000 5.600000
3 Petal.Length 34 10.8261765 6.473529 <5.7 >5.7
6 Petal.Width 28 3.4642857 6.264286 <2.05 >2.05
12 <leaf> 23 2.4260870 6.186957
13 <leaf> 5 0.2680000 6.620000
7 Petal.Length 6 0.4150000 7.450000 <6.35 >6.35
14 <leaf> 4 0.0275000 7.275000
15 <leaf> 2 0.0200000 7.800000
$where
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
8 17 6 16 13 5 13 13 13 10 13 13 6 14 5 13 9 9 13 16 14
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
10 10 13 10 10 10 13 5 8 13 16 13 14 5 13 13 6 13 6 9 5
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
8 6 9 9 14 5 8 13 8 17 14 6 10 8 10 13 16 6 10 5 9
24 93 39 69 124 66 112 148 50 56 1 37
6 10 5 13 13 13 13 13 5 13 8 8
$terms
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width
attr(,"variables")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"factors")
Sepal.Width Petal.Length Petal.Width
Sepal.Length 0 0 0
Sepal.Width 1 0 0
Petal.Length 0 1 0
Petal.Width 0 0 1
attr(,"term.labels")
[1] "Sepal.Width" "Petal.Length" "Petal.Width"
attr(,"order")
[1] 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: 0x00000000059480f0>
attr(,"predvars")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"dataClasses")
Sepal.Length Sepal.Width Petal.Length Petal.Width
"numeric" "numeric" "numeric" "numeric"
$call
snip.tree(tree = tree.output, nodes = c(19L, 18L, 5L, 16L, 13L,
12L))
$y
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
5.0 7.7 5.0 7.2 6.4 4.6 6.8 6.1 6.0 5.6 6.3 6.1 5.0 6.9 4.3 5.6 5.2 5.4 5.9 7.2 6.4
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
5.5 5.5 6.2 5.8 5.6 5.8 6.1 5.0 5.1 6.3 7.3 5.8 6.7 4.7 6.5 6.7 4.8 6.1 5.0 5.7 4.8
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
5.1 5.1 5.4 5.8 6.8 4.6 5.2 5.7 5.0 7.9 6.3 4.8 5.5 5.1 5.7 5.9 7.4 4.9 5.2 4.4 5.5
24 93 39 69 124 66 112 148 50 56 1 37
5.1 5.8 4.4 6.2 6.3 6.7 6.4 6.5 5.0 5.7 5.1 5.5
$weights
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[41] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
attr(,"class")
[1] "tree"
attr(,"xlevels")
attr(,"xlevels")$Sepal.Width
NULL
attr(,"xlevels")$Petal.Length
NULL
attr(,"xlevels")$Petal.Width
NULL
我需要使用不同的功能来保存模型吗?
谢谢。
您的对象已正确存储,但是打印出来的方式不同。对象 m.tree
是一个包含 class tree
:
的列表
R> class(m.tree)
[1] "tree"
当您打印对象时,它会查找函数 print.tree
(针对通用 print
函数)。此功能 仅 在您加载 tree
程序包后可用。
library(tree)
load("my model.RData")
m.tree
给你想要的。
如果你有兴趣,运行
getS3method("print", "tree")
查看打印方法
我安装了回归树。我尝试使用 save() 函数保存拟合模型,但如果我关闭 R,加载对象并调用它,我会得到不同的输出。
代码如下:
training.set=iris[,-5]
library(tree)
set.seed(123)
part1 = sample(1:nrow(training.set), round(nrow(training.set)/2))
part2 = setdiff(1:nrow(training.set), part1)
tree.output = tree("Sepal.Length~.", data=training.set[part1,],
control=tree.control(nobs=length(part1), minsize=2, mindev=0.001))
prune.t = prune.tree(tree.output, newdata=training.set[part2,])
plot(prune.t)
J = prune.t$size[prune.t$dev == min(prune.t$dev)]
J
m.tree = prune.tree(tree.output,best=J)
save(m.tree,file="my model.RData")
m.tree
输出:
node), split, n, deviance, yval
* denotes terminal node
1) root 75 49.54000 5.756
2) Petal.Length < 4.3 41 6.69800 5.161
4) Petal.Length < 3.55 31 3.82800 5.019
8) Sepal.Width < 3.45 17 1.04900 4.794
16) Petal.Length < 1.45 9 0.52220 4.644 *
17) Petal.Length > 1.45 8 0.09875 4.962 *
9) Sepal.Width > 3.45 14 0.86930 5.293
18) Sepal.Width < 3.85 8 0.17880 5.138 *
19) Sepal.Width > 3.85 6 0.24000 5.500 *
5) Petal.Length > 3.55 10 0.32000 5.600 *
3) Petal.Length > 4.3 34 10.83000 6.474
6) Petal.Length < 5.7 28 3.46400 6.264
12) Petal.Width < 2.05 23 2.42600 6.187 *
13) Petal.Width > 2.05 5 0.26800 6.620 *
7) Petal.Length > 5.7 6 0.41500 7.450
14) Petal.Length < 6.35 4 0.02750 7.275 *
15) Petal.Length > 6.35 2 0.02000 7.800 *
脚本的第二部分:
q()
rm(list=ls() )
load("my model.RData")
m.tree
输出:
$frame
var n dev yval splits.cutleft splits.cutright
1 Petal.Length 75 49.5448000 5.756000 <4.3 >4.3
2 Petal.Length 41 6.6975610 5.160976 <3.55 >3.55
4 Sepal.Width 31 3.8283871 5.019355 <3.45 >3.45
8 Petal.Length 17 1.0494118 4.794118 <1.45 >1.45
16 <leaf> 9 0.5222222 4.644444
17 <leaf> 8 0.0987500 4.962500
9 Sepal.Width 14 0.8692857 5.292857 <3.85 >3.85
18 <leaf> 8 0.1787500 5.137500
19 <leaf> 6 0.2400000 5.500000
5 <leaf> 10 0.3200000 5.600000
3 Petal.Length 34 10.8261765 6.473529 <5.7 >5.7
6 Petal.Width 28 3.4642857 6.264286 <2.05 >2.05
12 <leaf> 23 2.4260870 6.186957
13 <leaf> 5 0.2680000 6.620000
7 Petal.Length 6 0.4150000 7.450000 <6.35 >6.35
14 <leaf> 4 0.0275000 7.275000
15 <leaf> 2 0.0200000 7.800000
$where
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
8 17 6 16 13 5 13 13 13 10 13 13 6 14 5 13 9 9 13 16 14
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
10 10 13 10 10 10 13 5 8 13 16 13 14 5 13 13 6 13 6 9 5
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
8 6 9 9 14 5 8 13 8 17 14 6 10 8 10 13 16 6 10 5 9
24 93 39 69 124 66 112 148 50 56 1 37
6 10 5 13 13 13 13 13 5 13 8 8
$terms
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width
attr(,"variables")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"factors")
Sepal.Width Petal.Length Petal.Width
Sepal.Length 0 0 0
Sepal.Width 1 0 0
Petal.Length 0 1 0
Petal.Width 0 0 1
attr(,"term.labels")
[1] "Sepal.Width" "Petal.Length" "Petal.Width"
attr(,"order")
[1] 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: 0x00000000059480f0>
attr(,"predvars")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"dataClasses")
Sepal.Length Sepal.Width Petal.Length Petal.Width
"numeric" "numeric" "numeric" "numeric"
$call
snip.tree(tree = tree.output, nodes = c(19L, 18L, 5L, 16L, 13L,
12L))
$y
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
5.0 7.7 5.0 7.2 6.4 4.6 6.8 6.1 6.0 5.6 6.3 6.1 5.0 6.9 4.3 5.6 5.2 5.4 5.9 7.2 6.4
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
5.5 5.5 6.2 5.8 5.6 5.8 6.1 5.0 5.1 6.3 7.3 5.8 6.7 4.7 6.5 6.7 4.8 6.1 5.0 5.7 4.8
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
5.1 5.1 5.4 5.8 6.8 4.6 5.2 5.7 5.0 7.9 6.3 4.8 5.5 5.1 5.7 5.9 7.4 4.9 5.2 4.4 5.5
24 93 39 69 124 66 112 148 50 56 1 37
5.1 5.8 4.4 6.2 6.3 6.7 6.4 6.5 5.0 5.7 5.1 5.5
$weights
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[41] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
attr(,"class")
[1] "tree"
attr(,"xlevels")
attr(,"xlevels")$Sepal.Width
NULL
attr(,"xlevels")$Petal.Length
NULL
attr(,"xlevels")$Petal.Width
NULL
我需要使用不同的功能来保存模型吗? 谢谢。
您的对象已正确存储,但是打印出来的方式不同。对象 m.tree
是一个包含 class tree
:
R> class(m.tree)
[1] "tree"
当您打印对象时,它会查找函数 print.tree
(针对通用 print
函数)。此功能 仅 在您加载 tree
程序包后可用。
library(tree)
load("my model.RData")
m.tree
给你想要的。
如果你有兴趣,运行
getS3method("print", "tree")
查看打印方法