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:
Standards<-data.frame(ng_mL=c(0,0.4,1,4), Abs550nm=c(1.7535,1.5896,1.4285,0.9362)) LM.2<-lm(log(Standards[['Abs550nm']])~Standards[['ng_mL']]) Abs<-c(1.7812,1.7309,1.3537,1.6757,1.7409,1.7875,1.7533,1.8169,1.753,1.6721,1.7036,1.6707, 0.3903,0.3362,0.2886,0.281,0.3596,0.4122,0.218,0.2331,1.3292,1.2734) predict(object=LM.2, newdata=data.frame(Concentration=Abs))#using Abs 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 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
You must have a variable in
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
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
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
The behavior you see happens when R cannot find a correctly named variable in
; 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
in the name. R recognizes this as a function and refers to the variable
If the model is valid
log(Abs550nm) ~ ng_mL
and you want to find values
for the new values
, you need to invert the established model somehow.
source to share