简体   繁体   English

在R中存储/保存预测模型

[英]Store/Save Predictive Model in R

I would like to store the output of the following code. 我想存储以下代码的输出。 I tried list, data frame and vector but was not able to store it. 我尝试了列表,数据框和向量,但无法存储它。

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"

Any ideas on how to store it? 有关如何存储的任何想法?

One thing you can do is save the model output in its own file. 您可以做的一件事是将模型输出保存在自己的文件中。 An R object can be saved as an rds file. R对象可以保存为rds文件。 For example: 例如:

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

You can then load the object into your R environment: 然后,您可以将对象加载到R环境中:

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

TL;DR: Model is itself a list that contains all of the desired information and more. TL; DR: Model本身就是一个包含所有所需信息的列表。 Run str(Model) to see its components. 运行str(Model)查看其组件。 However, you can also extract just the information you want and save it in a list as follows: 但是,您也可以只提取所需的信息并将其保存在列表中,如下所示:

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)

See below for additional explanation. 请参阅下面的其他说明。



Model is an lda model object, which is a list containing all the output of the lda function. Model是一个lda模型对象,它是一个包含lda函数所有输出的列表。 When you type Model , R is calling the print "method" for that lda model object, which is a function called print.lda (typing Model in the console is equivalent to typing print(Model) , which runs the print.lda function on your model object). 键入Model ,R调用该lda模型对象的print “方法”,这是一个名为print.lda的函数(在控制台中输入Model相当于键入print(Model) ,它运行print.lda函数你的模型对象)。 So, you can look at the model object and the print.lda function to see what they're doing and then create your own summary object from that. 因此,您可以查看模型对象和print.lda函数以查看它们正在执行的操作,然后从中创建自己的摘要对象。

Look at code for print.lda 查看print.lda代码

The code for print.lda is below. print.lda的代码如下。 Note that print.lda is getting each of its outputs from the model object. 请注意, print.lda从模型对象获取其每个输出。 For example x$prior is the prior probabilities of each group. 例如, x$prior是每个组的先验概率。 We just need to create a function to extract each of elements we want. 我们只需要创建一个函数来提取我们想要的每个元素。

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) } 

Look at the model object returned by lda 查看lda返回的模型对象

Now let's create a model and look at the model object. 现在让我们创建一个模型并查看模型对象。 We don't actually need to do look at the model object here, because print.lda tells us what we need to know. 我们实际上不需要在这里查看模型对象,因为print.lda告诉我们需要知道什么。 However, knowing the structure of the model object can be helpful if you want to extract information that isn't returned by the standard extractor functions provided with a package. 但是,如果要提取包中提供的标准提取函数未返回的信息,则了解模型对象的结构会很有帮助。 Note the the model object is a list containing various types of information about the model. 请注意,模型对象是包含有关模型的各种类型信息的列表。

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" 

Extract the information we want and return it in a list 提取我们想要的信息并将其返回列表

The function below just mimics what print.lda does, but saves the results in a list, rather than printing it to the screen: 下面的函数只是模仿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))
}

Now run the function: 现在运行函数:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM