R pred () function returns wrong / too many values

I am trying to convert the Absorbance (Abs) values ​​to concentration (ng / ml) based on an established linear model and a standard curve. I was planning to do this using the pred () function. I am having trouble getting the prediction () to return the results I want. Here's an example of my code:


        newdata=data.frame(Concentration=Abs[1]))#using Abs[1] as an example, but I eventually want predictions for all values in Abs


Running these last lines gives this output:

> predict(object=LM.2,
+         newdata=data.frame(Concentration=Abs[1]))
         1          2          3          4 
 0.5338437  0.4731341  0.3820697 -0.0732525 
Warning message:
'newdata' had 1 row but variables found have 4 rows 


This doesn't sound like the result I want. I am trying to get one predicted concentration value for each Absorbance (Abs) record. It would be nice to predict all the records at once and add them to the existing dataframe, but I can't even get it to give me one value correctly. I've read a lot of threads here, web pages found on google and all the help files, and for the life of me I can't figure out what's going on with this feature. Any help would be appreciated, thanks.


source to share

1 answer

You must have a variable in newdata

that has the same name as the model formula used to initially fit the model.

You have two errors:

  • You are not using a variable in newdata

    with the same name as the covariate used to match the model, and
  • You make it difficult to solve the problem because you are overusing the formula interface.

Don't match your model as follows:

mod <- lm(log(Standards[['Abs550nm']])~Standards[['ng_mL']])


suitable for your model as follows

mod <- lm(log(Abs550nm) ~ ng_mL, data = standards)


Isn't that too readable?

For prediction, you need a data frame with a variable ng_mL


predict(mod, newdata = data.frame(ng_mL = c(0.5, 1.2)))


You may now have a third error. You seem to be trying to predict with new values ​​of Absorbance, but the way you fit the model, Absorbance is the answer variable. You will need to provide new values ​​for ng_mL


The behavior you see happens when R cannot find a correctly named variable in newdata

; it returns set values ​​from a model or prediction from observed data.

This makes me think you have a back formula. Did you mean:

mod2 <- lm(ng_mL ~ log(Abs550nm), data = standards)


?? In this case, you will need

predict(mod2, newdata = data.frame(Abs550nm = c(1.7812,1.7309)))


say. Note that you do not need to include the bit log()

in the name. R recognizes this as a function and refers to the variable Abs550nm

for you.

If the model is valid log(Abs550nm) ~ ng_mL

and you want to find values ng_mL

for the new values Abs550nm

, you need to invert the established model somehow.



All Articles