Srijit Mukherjee
Published in

Srijit Mukherjee

How to teach R to copy your handwriting?

Hey. Welcome guys to whysrijit. This is the place, where we ask more whys to become wise.

Today we learn how to Make R learn your Handwriting?

We will demonstrate this by a simple example. But, before that let’s try to understand, let’s try to understand the process and then proceed towards the nitty-gritty.

The Steps

  1. Your Sample Handwriting / Drawing — The Data for R algos.
  2. The Algorithm
  3. The Animation

Let’s start.

Your Sample Handwriting / Drawing

Before jumping directly into the process, let’s try to understand the data, we need.

We don’t need the drawing / or handwriting pixels only. We need the way it is drawn.

More formally you need the sequence of the (x,y) coordinates that are drawn by your hand.

How, to get that? This was my most time-consuming part. You can get it here from this site. (You are welcome :p)

Let’s take an example. Let’s say, I want to draw a circle. I get the sequence of the clicks of my data from the above site. Save it to R and start working.

The Algorithm

Now, you have the data and you have errors naturally, and there marches in Statistics. You can see the error in the data I collected. But don’t worry, the data is collected in a sequential form (as mentioned) in the data.

Now, you want to draw a circle through it like the following.

But, how do we draw the circle? That is where spline regression comes in.

But, regression is for estimating a function, but here it is not a function. Fuck!

Don’t worry. Mathematics is there. What is the definition of a curve?

A curve in 2 D is a function f: [0,1] →R², where f(t) = (a(t), b(t)).

Now, we are trying to estimate f(t), which is not a function from R →R.

So, here we will instead estimate a(t) and b(t) individually like the following.

The X coordinate estimation

The Y coordinate estimation

Now, we know how to estimate a function f: R →R by spline regression right?

Let’s move into the codes.

The Codes and the Animation

seq = 1:length(Data$x)
data = data.frame(seq,data$x,data$y)
x = data[,c(1,2)]
y = data[,c(1,3)]
xfit <- gam(x ~ s(seq, bs="cr") , data=x)
#General Additive Model Regression using Cubic Splines
t = seq(1,length(x[,1]), length.out = 1000)
xpred = predict(xfit, data.frame(seq = t))
yfit <- gam(y ~ s(seq, bs="cr") , data=y)
t = seq(1,length(y[,1]), length.out = 1000)
ypred = predict(yfit, data.frame(seq = t))
newdata = data.frame(t,xpred,ypred)anim <-
ggplot(s_newdata, aes(xpred, ypred)) +
geom_path(size = 1, colour = "red")+
geom_point(colour = "blue", size = 2) +
theme_minimal() +
labs(title = 'Drawing my Circle')+
animate(anim, nframes = 30, type="cairo")

A small tribute to Cheenta Ganit Kendra and Cheenta Statistics Department.

Thanks for stopping by.

If you like the article, don’t forget to clap, follow Srijit Mukherjee for more such exciting stuff, and share among your enthusiastic peers. You can connect to me over Linkedin.


©Srijit Mukherjee 2020



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Srijit Mukherjee

Srijit Mukherjee

A whys gent, who seeks liberation by the medium of sharing.