I have 4 columns of measurement (distance) data collected using a device that measures a voltage gap on separate channels.
CH_1 CH_2 CH_3 CH_4
-40160 -38180 -63972 -54560
-40160 -38140 -63972 -54552
-40168 -38168 -63972 -54568
-40172 -38116 -63984 -54544
-40160 -38184 -63988 -54568
-40168 -38172 -63980 -54564
-40168 -38156 -63972 -54552
-40172 -38152 -63984 -54532
-40156 -38176 -63968 -54552
-40168 -38132 -63976 -54544
-40172 -38136 -63992 -54556
-40168 -38140 -63984 -54528
In order to convert these voltages to a more interpretable physical distance, the sensors were calibrated with standards of a known distance in mm. These were read into R as a dataframe called calibrations
x CH_1 CH_2 CH_3 CH_4
1mm -49000 -42000 -54000 -49000
2mm -46000 -36000 -46000 -44000
3mm -35000 -32000 -42000 -33000
4mm -30000 -28000 -38000 -27000
5mm -26000 -22000 -29000 -20000
With those values, using the broom
package, I ran several simple linear regressions and stored them.
calibrations<-melt(calibrations, id.vars = "x")
lms<-calibrations %>% group_by(variable) %>% do(tidy(lm(value~x, data=.)))
This gives the output of:
>lms
>variable term estimate std.error statistic p.value
1 CH_1 (Intercept) -55800. 2298. -24.3 0.000153
2 CH_1 x 6200. 693. 8.95 0.00294
3 CH_2 (Intercept) -46400. 766. -60.6 0.00000991
4 CH_2 x 4800. 231. 20.8 0.000244
5 CH_3 (Intercept) -59200 1755. -33.7 0.0000573
6 CH_3 x 5800 529. 11.0 0.00163
7 CH_4 (Intercept) -57100. 1567. -36.4 0.0000455
8 CH_4 x 7500. 473. 15.9 0.000544
My question is: How can I use lapply
or similar to pass the linear model coefficent values to solve for "x" for all of the voltage values ("y") in the original dataset?
- i.e. for CH_1 x=(y+55800)/6200. This would give a value of 2.52 mm for the first voltage recording of CH_1 - (x=(-40160+55800)/6200).
Secondly, is there a way to this automatically for all for voltage variables at the same time?