Amara
Amara

Reputation: 21

What is R code or function that is equivalent to "Trend Function" in an excel sheet?

I am just wondering what function or R code I can use in R that would be equivalent to 'Trend Function' in a microsoft excel sheet. As trend function in excel is equal to "Returns values along a linear trend. TREND fits a straight line (using the method of least squares) to the arrays known_y's and known_x's. Returns the y-values along that line for the array of new_x's that you specify". I have two column in excel sheet columnY= "absorbances" and columnX = "concentrations" by using these two columns I need to calculate new_x's that returns the y-value along that line for the array of new_x's that I specify. My new_x's are unknown absorbances. What is the easiest way to do this in 'R' as I am spending much time to do it in Excel sheet. I have bunch of (near 300) new_x's values in a excel sheet that needed to be calculate by using columnY and columnX.

Upvotes: 1

Views: 1845

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269644

TREND(y, x) in Excel gives the same result as the following (where x and y are numeric vectors of the same length):

fitted(lm(y ~ x))

or to predict different new values (where x and y are as above and new_values is a numeric vector of x values for which the y values are to be predicted):

predict(lm(y ~ x), list(x = new_values))

See ?lm, ?fitted and ?predict for more information.

Note that if x and y each have length 2 then this would also work:

approx(x, y, xout = new_values)$y

Upvotes: 1

Related Questions