How can I calculate acceleration using linear regression with `ggplot2`

I have the following data:

t = seq(0, 0.45, 0.05)
d = c(0, 0.028, 0.055, 0.111, 0.202, 0.295, 0.435, 0.589, 0.771, 0.980)

This is for physics. t represents time and d represents displacement:

Time (s) Displacement (m)
0.00 0.000
0.05 0.028
0.10 0.055
0.15 0.111
0.20 0.202
0.25 0.295
0.30 0.435
0.35 0.589
0.40 0.771
0.45 0.980

In the Desmos graphing calculator, I can simply insert the above table and then add the expression y1 ~ 0.5 ax1^2 where x1 is t (time) and y1 is d (displacement). What it will do is draw a curve of best fit and then give me the value for acceleration. I want to do the same thing with ggplot2.

library(ggplot2)

ggplot(data.frame(t, d), aes(x = t, y = d)) +
	theme_linedraw() + theme(plot.title = element_text(hjust = 0.5)) +
	geom_smooth() + geom_point()

This is the result:

It is almost exactly what I want, but I want a label somewhere in the graph to show what the acceleration is similar to what I did in Desmos. Also, I want to use the same equation I used in Desmos for the regression curve (y1 ~ 0.5 ax1^2). I know that geom_smooth() takes the argument formula but it gave me an error when trying to use that.

Here's one way to do this:

t = seq(0, 0.45, 0.05)
d = c(0, 0.028, 0.055, 0.111, 0.202, 0.295, 0.435, 0.589, 0.771, 0.980)


library(ggplot2)

m <- lm(d~I(t^2)-1) 
# I means to literally square t before doing the regression and "-1" removes an intercept 
# This fits y~z*x^2 instead of y~0.5*a*x^2 so a=z/2

coef(m)
#>   I(t^2) 
#> 4.828775

ggplot(data.frame(t, d), aes(x = t, y = d)) +
  theme_linedraw() + theme(plot.title = element_text(hjust = 0.5)) +
  geom_point() +
  geom_function(fun=function(x){coef(m)*x^2}) +
  ggtitle(paste("a=", coef(m)/2))

Created on 2022-10-15 with reprex v2.0.2

2 Likes

This works but it seems like it gave the incorrect result. On Desmos, I got a = 9.68056 which is not the same result here. a in this context is the acceleration due to gravity so it should be somewhere near 9.8.

It is just a simple error. The relationship between a and z is a = z*2.

t = seq(0, 0.45, 0.05)
d = c(0, 0.028, 0.055, 0.111, 0.202, 0.295, 0.435, 0.589, 0.771, 0.980)


library(ggplot2)

m <- lm(d~I(t^2)-1) 
# I means to literally square t before doing the regression and "-1" removes an intercept 
# This fits y~z*x^2 instead of y~0.5*a*x^2 so a=z*2

coef(m)
#>   I(t^2) 
#> 4.828775

ggplot(data.frame(t, d), aes(x = t, y = d)) +
  theme_linedraw() + theme(plot.title = element_text(hjust = 0.5)) +
  geom_point() +
  geom_function(fun=function(x){coef(m)*x^2}) +
  ggtitle(paste("a=", coef(m)*2))

Created on 2022-10-16 with reprex v2.0.2

1 Like

@FJCC thanks for the working solution.

The solutions is @StatSteph's, I only noticed an algebra error.

1 Like

Yeah thanks to @StatSteph too.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.