predict in combination with by

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

predict in combination with by

Progressive
Hi,

I'm trying to use predict within the by function but I do not succeed.

I have the data frame before_database.frame which contains different subsets (identified by their serial numbers). These I want to fit separately and receive the prediction of a value from the fit.

For a subset alone I'm able to do it but I don't know how to do it in combination with by.

Single set:
before_database.frame<- read.table("APD_data.txt",
                                  header = TRUE,
                                  sep = "",
                                  dec="."
                                 )

Single_APD.frame<- before_database.frame[before_database.frame$Serial_number==912009913, ]

fit<- lm(Voltage ~ poly(Amplification,2), data=Single_APD.frame)

New_Amp_Single_APD.frame<- data.frame(Amplification=150)

predict(fit, New_Amp_Single_APD.frame)

Now I would like to this for all subsets and I think it should be somehow like follows:

Read data:
before_database.frame<- read.table(
                                  "APD_data.txt",
                                   header = TRUE,
                                   sep = "",
                                   dec="."
                                  )

Group and fit:
grouped.frame<- do.call(
                        'rbind',
                        by(before_database.frame, before_database.frame$Serial_number,
                        function(d) (
                                    fit<- lm(Amplification ~ Voltage, data = d)
                                    New_Amp_Single_APD.frame<- data.frame(Amplification=150),
                                   predict(fit, New_Amp_Single_APD.frame)
                            )
                        )


Thank you in advance!