获取R中图的二次方程项 [英] Get quadratic equation term of a graph in R

查看:72
本文介绍了获取R中图的二次方程项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到我在R中绘制的图的二次方程项. 当我在excel中执行此操作时,该术语会出现在图表上的文本框中,但我不确定如何将其移至单元格中以供后续使用(适用于需要校准的值),或者不确定如何在R中要求它.如果它在R中是可召唤的,是否可以另存为对象以用于将来的计算?

I need to find the quadratic equation term of a graph I have plotted in R. When I do this in excel, the term appears in a text box on the chart but I'm unsure how to move this to a cell for subsequent use (to apply to values requiring calibrating) or indeed how to ask for it in R. If it is summonable in R, is it saveable as an object to do future calculations with?

这似乎应该是R中的直接要求,但是我找不到任何类似的问题.在此先非常感谢任何人可以提供的任何帮助.

This seems like it should be a straightforward request in R, but I can't find any similar questions. Many thanks in advance for any help anyone can provide on this.

推荐答案

所有答案都提供了您想做的事情的各个方面,但到目前为止,还没有将它们统统结合在一起.让我们考虑汤姆·利普特(Tom Liptrot)的答案示例:

All the answers provide aspects of what you appear at want to do, but non thus far brings it all together. Lets consider Tom Liptrot's answer example:

fit <- lm(speed ~ dist + I(dist^2), cars)

这为我们提供了一个拟合的线性模型,变量dist具有二次方.我们使用coef()提取函数提取模型系数:

This gives us a fitted linear model with a quadratic in the variable dist. We extract the model coefficients using the coef() extractor function:

> coef(fit)
 (Intercept)         dist    I(dist^2) 
 5.143960960  0.327454437 -0.001528367

因此,您拟合的方程式(由于打印而需要四舍五入):

So your fitted equation (subject to rounding because of printing is):

\ hat {speed} = 5.143960960 +(0.327454437 * dist)+(-0.001528367 * dist ^ 2)

\hat{speed} = 5.143960960 + (0.327454437 * dist) + (-0.001528367 * dist^2)

(其中\ hat {speed}是响应的合适值,即速度).

(where \hat{speed} is the fitted values of the response, speed).

如果您想将此拟合方程应用于某些数据,那么我们可以编写自己的函数来做到这一点:

If you want to apply this fitted equation to some data, then we can write our own function to do it:

myfun <- function(newdist, model) {
    coefs <- coef(model)
    res <- coefs[1] + (coefs[2] * newdist) + (coefs[3] * newdist^2)
    return(res)
}

我们可以这样应用此功能:

We can apply this function like this:

> myfun(c(21,3,4,5,78,34,23,54), fit)
[1] 11.346494  6.112569  6.429325  6.743024 21.386822 14.510619 11.866907
[8] 18.369782

对于距离(dist)的一些新值,这似乎是您想从Q开始执行的操作.但是,在R中,我们通常不会执行这样的操作,因为,为什么用户必须这样做知道如何从R中可以拟合的所有不同类型的模型中形成拟合值或预测值?

for some new values of distance (dist), Which is what you appear to want to do from the Q. However, in R we don't do things like this normally, because, why should the user have to know how to form fitted or predicted values from all the different types of model that can be fitted in R?

在R中,我们使用标准方法和提取器函数.在这种情况下,如果要将Excel显示的等式"应用于所有数据以获取此回归的拟合值,则在R中,我们将使用fitted()函数:

In R, we use standard methods and extractor functions. In this case, if you want to apply the "equation", that Excel displays, to all your data to get the fitted values of this regression, in R we would use the fitted() function:

> fitted(fit)
        1         2         3         4         5         6         7         8 
 5.792756  8.265669  6.429325 11.608229  9.991970  8.265669 10.542950 12.624600 
        9        10        11        12        13        14        15        16 
14.510619 10.268988 13.114445  9.428763 11.081703 12.122528 13.114445 12.624600 
       17        18        19        20        21        22        23        24 
14.510619 14.510619 16.972840 12.624600 14.951557 19.289106 21.558767 11.081703 
       25        26        27        28        29        30        31        32 
12.624600 18.369782 14.057455 15.796751 14.057455 15.796751 17.695765 16.201008 
       33        34        35        36        37        38        39        40 
18.688450 21.202650 21.865976 14.951557 16.972840 20.343693 14.057455 17.340416 
       41        42        43        44        45        46        47        48 
18.038887 18.688450 19.840853 20.098387 18.369782 20.576773 22.333670 22.378377 
       49        50 
22.430008 21.93513

如果您要将模型方程式应用于一些不适合用于模型的新数据值,那么我们需要从模型中获得预测.这是通过predict()函数完成的.使用我在上面的myfun中插入的距离,这就是我们将以更加以R为中心的方式实现的方式:

If you want to apply your model equation to some new data values not used to fit the model, then we need to get predictions from the model. This is done using the predict() function. Using the distances I plugged into myfun above, this is how we'd do it in a more R-centric fashion:

> newDists <- data.frame(dist = c(21,3,4,5,78,34,23,54))
> newDists
  dist
1   21
2    3
3    4
4    5
5   78
6   34
7   23
8   54
> predict(fit, newdata = newDists)
        1         2         3         4         5         6         7         8 
11.346494  6.112569  6.429325  6.743024 21.386822 14.510619 11.866907 18.369782

首先,我们创建一个名为"dist"的新数据框,其中包含我们希望从模型中获得预测的新距离.重要的是要注意,我们在此数据框中包含一个变量,该变量的名称与创建拟合模型时使用的变量的名称相同.这个新的数据框必须包含用于拟合模型的所有变量,但是在这种情况下,我们只有一个变量dist.还要注意,我们不需要包含有关dist ^ 2的任何内容. R会替我们处理.

First up we create a new data frame with a component named "dist", containing the new distances we want to get predictions for from our model. It is important to note that we include in this data frame a variable that has the same name as the variable used when we created our fitted model. This new data frame must contain all the variables used to fit the model, but in this case we only have one variable, dist. Note also that we don't need to include anything about dist^2. R will handle that for us.

然后,我们使用predict()函数,为其提供拟合模型,并提供刚创建为参数'newdata'的新数据框,从而为我们提供新的预测值,该值与我们之前手工完成的值匹配.

Then we use the predict() function, giving it our fitted model and providing the new data frame just created as argument 'newdata', giving us our new predicted values, which match the ones we did by hand earlier.

我掩饰的是predict()fitted()实际上是一组完整的功能.有lm()模型,glm()模型等版本.它们被称为泛型函数,具有方法(如果需要,可提供版本)用于几种不同类型的目的.用户通常只需要记住使用fitted()predict()等,而R会为您提供的拟合模型类型使用正确的方法.以下是基本R中用于fitted()泛型函数的一些方法:

Something I glossed over is that predict() and fitted() are really a whole group of functions. There are versions for lm() models, for glm() models etc. They are known as generic functions, with methods (versions if you like) for several different types of object. You the user generally only need to remember to use fitted() or predict() etc whilst R takes care of using the correct method for the type of fitted model you provide it. Here are some of the methods available in base R for the fitted() generic function:

> methods(fitted)
[1] fitted.default*       fitted.isoreg*        fitted.nls*          
[4] fitted.smooth.spline*

   Non-visible functions are asterisked

根据您已加载的其他软件包,您可能会获得比这更多的收益. *只是意味着您不能直接引用这些功能,必须使用fitted(),R可以确定要使用的功能.请注意,没有用于lm()对象的方法.这种类型的对象不需要特殊的方法,因此default方法将被使用并且很合适.

You will possibly get more than this depending on what other packages you have loaded. The * just means you can't refer to those functions directly, you have to use fitted() and R works out which of those to use. Note there isn't a method for lm() objects. This type of object doesn't need a special method and thus the default method will get used and is suitable.

这篇关于获取R中图的二次方程项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆