在R

时间:2016-09-13 16:17:01

标签: r

我想存储以下代码的输出。我尝试了列表,数据框和向量,但无法存储它。

Model<-lda( y ~ Trend+Class+F1+F4+ATR+macd_signal+macd1+F5 ,data=x)


> Model

Call:
lda(y ~ Trend + Class + F1 + F4 + ATR + macd_signal + macd1 + 
    F5, data = x)

Prior probabilities of groups:

     LOSS    PROFIT 
0.4981818 0.5018182 

Group means:

       TrendBull   ClassUP       F1       F4      ATR   macd_signal       macd1       F5
LOSS   0.5450122 0.3990268 1480.451 1481.672 11.64657 -0.0005850151  0.01542818 1478.567
PROFIT 0.5000000 0.4082126 1487.280 1486.707 12.25799 -0.0304256947 -0.03845741 1489.620

Coefficients of linear discriminants:

                     LD1
TrendBull   -0.033267160

ClassUP      0.151291378

F1          -0.003215276

F4          -0.042431558

ATR          0.082615338

macd_signal  0.090182055

macd1        2.637216918

F5           0.045956343

> class(Model)

[1] "lda"

> typeof(Model)

[1] "list"

有关如何存储它的任何想法?

2 个答案:

答案 0 :(得分:4)

您可以做的一件事是将模型输出保存在自己的文件中。 R对象可以保存为rds文件。例如:

saveRDS(model, file = "your path/filename.rds")

然后,您可以将对象加载到R环境中:

mod <- readRDS("your path/filename.rds")

答案 1 :(得分:2)

TL; DR: Model本身就是一个包含所有所需信息的列表。运行str(Model)以查看其组件。但是,您也可以只提取所需的信息并将其保存在列表中,如下所示:

my_lda_smry = function(x) {
  list(Call=x$call, Prior=x$prior, `Group Means`=x$means, 
     Coefficients=x$scaling,
     `Proportion of Trace`= round(x$svd^2/sum(x$svd^2), 4))
}

Model_summary = my_lda_smry(Model)

有关其他说明,请参阅下文。

Modellda模型对象,它是包含lda函数的所有输出的列表。当您键入Model时,R正在为该lda模型对象调用print“方法”,这是一个名为print.lda的函数(在控制台中键入Model相当于键入print(Model),它在模型对象上运行print.lda函数。因此,您可以查看模型对象和print.lda函数以查看它们正在执行的操作,然后从中创建自己的摘要对象。

查看print.lda

的代码

print.lda的代码如下。请注意,print.lda从模型对象获取其每个输出。例如,x$prior是每个组的先验概率。我们只需要创建一个函数来提取我们想要的每个元素。

getAnywhere(print.lda)
function (x, ...) 
{
    if (!is.null(cl <- x$call)) {
        names(cl)[2L] <- ""
        cat("Call:\n")
        dput(cl, control = NULL)
    }
    cat("\nPrior probabilities of groups:\n")
    print(x$prior, ...)
    cat("\nGroup means:\n")
    print(x$means, ...)
    cat("\nCoefficients of linear discriminants:\n")
    print(x$scaling, ...)
    svd <- x$svd
    names(svd) <- dimnames(x$scaling)[[2L]]
    if (length(svd) > 1L) {
        cat("\nProportion of trace:\n")
        print(round(svd^2/sum(svd^2), 4L), ...)
    }
    invisible(x)
}

查看lda

返回的模型对象

现在让我们创建一个模型并查看模型对象。我们实际上不需要在这里查看模型对象,因为print.lda告诉我们需要知道什么。但是,如果要提取包中提供的标准提取函数未返回的信息,则了解模型对象的结构会很有帮助。请注意,模型对象是包含有关模型的各种类型信息的列表。

library(MASS)

model = lda(mpg ~ wt + hp + carb + cyl, data=mtcars)

str(model)
List of 10
 $ prior  : Named num [1:25] 0.0625 0.0312 0.0312 0.0312 0.0312 ...
  ..- attr(*, "names")= chr [1:25] "10.4" "13.3" "14.3" "14.7" ...
 $ counts : Named int [1:25] 2 1 1 1 1 2 1 1 1 1 ...
  ..- attr(*, "names")= chr [1:25] "10.4" "13.3" "14.3" "14.7" ...
 $ means  : num [1:25, 1:4] 5.34 3.84 3.57 5.34 3.57 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:25] "10.4" "13.3" "14.3" "14.7" ...
  .. ..$ : chr [1:4] "wt" "hp" "carb" "cyl"
 $ scaling: num [1:4, 1:4] 4.668 -0.0115 -3.6744 -3.8415 4.2625 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:4] "wt" "hp" "carb" "cyl"
  .. ..$ : chr [1:4] "LD1" "LD2" "LD3" "LD4"
 $ lev    : chr [1:25] "10.4" "13.3" "14.3" "14.7" ...
 $ svd    : num [1:4] 10.51 3.42 1.49 1.05
 $ N      : int 32
 $ call   : language lda(formula = mpg ~ wt + hp + carb + cyl, data = mtcars)
 $ terms  :Classes 'terms', 'formula'  language mpg ~ wt + hp + carb + cyl
  .. ..- attr(*, "variables")= language list(mpg, wt, hp, carb, cyl)
  .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:5] "mpg" "wt" "hp" "carb" ...
  .. .. .. ..$ : chr [1:4] "wt" "hp" "carb" "cyl"
  .. ..- attr(*, "term.labels")= chr [1:4] "wt" "hp" "carb" "cyl"
  .. ..- attr(*, "order")= int [1:4] 1 1 1 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(mpg, wt, hp, carb, cyl)
  .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
  .. .. ..- attr(*, "names")= chr [1:5] "mpg" "wt" "hp" "carb" ...
 $ xlevels: Named list()
 - attr(*, "class")= chr "lda"

提取我们想要的信息并将其返回列表

下面的函数只是模仿print.lda的作用,但将结果保存在列表中,而不是将其打印到屏幕上:

my_lda_smry = function(x) {
  list(Call=x$call, Prior=x$prior, `Group Means`=x$means, 
     Coefficients=x$scaling,
     `Proportion of Trace`= round(x$svd^2/sum(x$svd^2), 4))
}

现在运行功能:

m.smry = my_lda_smry(model)
$Call
lda(formula = mpg ~ wt + hp + carb + cyl, data = mtcars)

$Prior
   10.4    13.3    14.3    14.7      15    15.2    15.5    15.8    16.4    17.3    17.8    18.1    18.7    19.2    19.7      21 
0.06250 0.03125 0.03125 0.03125 0.03125 0.06250 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.06250 0.03125 0.06250 
   21.4    21.5    22.8    24.4      26    27.3    30.4    32.4    33.9 
0.06250 0.03125 0.06250 0.03125 0.03125 0.03125 0.06250 0.03125 0.03125 

$`Group Means`
         wt    hp carb cyl
10.4 5.3370 210.0  4.0   8
13.3 3.8400 245.0  4.0   8
14.3 3.5700 245.0  4.0   8
14.7 5.3450 230.0  4.0   8
15   3.5700 335.0  8.0   8
15.2 3.6075 165.0  2.5   8
15.5 3.5200 150.0  2.0   8
15.8 3.1700 264.0  4.0   8
16.4 4.0700 180.0  3.0   8
17.3 3.7300 180.0  3.0   8
17.8 3.4400 123.0  4.0   6
18.1 3.4600 105.0  1.0   6
18.7 3.4400 175.0  2.0   8
19.2 3.6425 149.0  3.0   7
19.7 2.7700 175.0  6.0   6
21   2.7475 110.0  4.0   6
21.4 2.9975 109.5  1.5   5
21.5 2.4650  97.0  1.0   4
22.8 2.7350  94.0  1.5   4
24.4 3.1900  62.0  2.0   4
26   2.1400  91.0  2.0   4
27.3 1.9350  66.0  1.0   4
30.4 1.5640  82.5  2.0   4
32.4 2.2000  66.0  1.0   4
33.9 1.8350  65.0  1.0   4

$Coefficients
             LD1          LD2         LD3         LD4
wt    4.66796895  4.262520788  0.35307402 -0.67013561
hp   -0.01149489  0.005714994  0.04376624  0.01627358
carb -3.67441417 -0.581458148 -0.44870373 -0.83522067
cyl  -3.84149993 -0.911662765 -1.52258858  0.37350681

$`Proportion of Trace`
[1] 0.8804 0.0930 0.0178 0.0088