8 Appendix 3: Data analysis using open-source R language

Warning: You should read the whole handbook before getting your hands dirty with the data analysis.

R is a popular opensource programming language specialized in statistical operations. Rstudio is a freely available IDE (Integrated Development environment) for the R language.

This whole handbook is itself a Rstudio project, accessible at the following link and written using Bookdown

The following code chunks represents an efficient way of analyzing the data.

It’s strongy suggested to run this code on a Unix compatible machine, such as Linux or macOS. Windows operative systems may create issues with file paths.

8.1 Downloading the handbook

This handbook is publicly available at the following link https://github.com/fermiumlabs/hall-effect-handbook

You can download always updated as a .zip archive at this link

8.2 Code chunks

The following is a code chunk:

print("I am code, yet i output text")
## [1] "I am code, yet i output text"

You can run a code chunk in many ways. The easiest, is to open this file (called 13-Hall_Handbook.Rmd) with Rstudio (better if you open the whole .Rproj project file first) and click on the little “play” button near the code chunk. You can also use “Run All” which will run all code chunks sequentially.

The code chunk can output text, warning messages, and sometimes graphics. We’ve hidden irrelevant output so it doesn’t clutter up the handbook.

This document can also be converted into HTML or PDF with all the generated outputs. You can also re-build this manual using your data if you so desire, altough this requires having installed Pandoc, Latex and Make.

8.3 Preliminary operations

First, we need to install the required packages and load them:

# Install if not present, then load the package
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')
if (!require('here')) install.packages('here'); library('here')
if (!require('gridExtra')) install.packages('gridExtra'); library('gridExtra')
if (!require('ggpubr')) install.packages('ggpubr'); library('ggpubr')
if (!require('ggthemes')) install.packages('ggthemes'); library('ggthemes')

The input data needs to be loaded as well. In the GitHub repository for this manual we’ve already included some default data at the path “hall-effect-handbook/analysis/data/ltk-hall-ge-current/experiment_1.csv”. You’re invited to add your data in the folder “ltk-hall-ge current” and change the name of the variable “filename” in the following code chunk to match yours.

The file needs to be exported in CSV from the software NNG Logger ltk-hall-ge comes with.

# CHANGE ME !
filename = "experiment_1.csv"

# Import the csv file
data = read.csv(here('analysis','data','ltk-hall-ge current', filename),header = T)

Let’s also decide the directory where we want our file to be saved. We’ve reserved a directory called “plot_out” in this repository. The second part of the code will print the full path, and will be different depending on the computer the code is ran on.

# Where to save the files
outdir = "plot_out"

# Print the full directory
cat(sprintf("The file will be saved to %s", here()))
## The file will be saved to /pipeline/source

8.4 Analyzing the data

We probably have tons of datapoints. Millions to be precise.

Let’s take a look at them before doing any matematical operation. The values of \(V_H\), \(V_R\), \(R\), temperature should be quite clean. The values if \(I\) may look noisy due to to high resolution of the measure and autoscale of the plot. We can ignore the values of \(B\) for now.

plot(data$Vh, ylab = "Vh")

plot(data$Vr, ylab = "Vr")

plot(data$temp, ylab = "Temp")

plot(data$R, ylab = "R")

plot(data$I, ylab = "I")

#plot(data$time,data$B)

A quick summary of statistical indicators can also be useful:

summary(data)
##       time                 Vh                 temp             Vr        
##  Min.   :1.514e+12   Min.   :-0.061875   Min.   :192.4   Min.   :0.1142  
##  1st Qu.:1.514e+12   1st Qu.:-0.056531   1st Qu.:281.6   1st Qu.:0.1995  
##  Median :1.514e+12   Median :-0.044969   Median :338.3   Median :0.3623  
##  Mean   :1.514e+12   Mean   :-0.031986   Mean   :334.0   Mean   :0.3604  
##  3rd Qu.:1.514e+12   3rd Qu.: 0.002938   3rd Qu.:397.8   3rd Qu.:0.5302  
##  Max.   :1.514e+12   Max.   : 0.036844   Max.   :423.2   Max.   :0.5998  
##        I                 R                B          
##  Min.   :0.01885   Min.   : 5.923   Min.   :0.01742  
##  1st Qu.:0.01909   1st Qu.:10.483   1st Qu.:0.01844  
##  Median :0.01911   Median :19.007   Median :0.01867  
##  Mean   :0.01912   Mean   :18.853   Mean   :0.01870  
##  3rd Qu.:0.01915   3rd Qu.:27.692   3rd Qu.:0.01891  
##  Max.   :0.01933   Max.   :31.360   Max.   :0.02039

We might need to manually crop our data to remove some useless datapoints. We’ll also convert the “time” field from milliseconds to seconds and subtract the first time so it starts from zero.

# Remove first 200 values. You can remove this line if your data is clean from the start
data<-data[-(1:200),]

# Convert time
data$time<-(data$time)/1000 # from milliseconds to seconds
data$time<-(data$time-data$time[1]) # remove initial offset

Let’s take a look how \(Vh\) and \(Vr\) change in function of the temperature:

# Temperature vs Vh plot
plot(data$temp, -1*data$Vh, xlab = "Temperature (degrees K)", ylab = "Vh")

# Temperature vs Vr plot
plot(data$temp, -1*data$Vr, xlab = "Temperature (degrees K)", ylab = "Vr")

The graphs looks quite ugly. The following code makes them once more, but using a more extensive and customizeable plotting software called Ggplot2 at the expense of a bit more complex code. We’ll smooth the datapoints by using a spline.

# Temperature vs Vh
ggplot()+ geom_line(data=data,aes(temp,-Vh),col="blue")+
  xlab(paste("Temperature (degrees K)" )) + ylab("Vhall (V)")+
  ggtitle("Temperature VS Vhall")+
  theme_tufte()

ggsave(here(outdir, "temp-vs-vh-1.png"), device = "png")


# Temperature vs vr
ggplot()+ geom_line(data=data,aes(temp,Vr),col="blue")+
  xlab(paste("Temperature (degrees K)" ))+ylab("Vr (V)")+
  ggtitle("Temperature VS Vr")+
  theme_tufte()

ggsave(here(outdir, "temp-vs-vr-1.png"), device = "png")


# Smooth the data using a spline
tempVSVhall.smooth <- with(data,smooth.spline(temp,-Vh))
tempVSVres.smooth <- with(data,smooth.spline(temp,Vr))

# Temperature vs Vh, smooth
ggplot()+ geom_line(data=with(tempVSVhall.smooth,data.frame(x,y)),aes(x,y),col="blue")+
  xlab(paste( "Temperature (degrees K)" ))+ylab("Vh (V)")+
  ggtitle("Temperature VS Vh, smoothed")+
  theme_tufte()

ggsave(here(outdir, "temp-vs-vh-smooth-1.png"), device = "png")


# Temperature vs Vr, smooth
ggplot()+ geom_line(data=with(tempVSVres.smooth,data.frame(x,y)),aes(x,y),col="blue")+
  xlab(paste("Temperature (degrees K)" ))+ylab("Vr (V)")+
  ggtitle("Temperature VS Vr, smoothed")+
  theme_tufte()

ggsave(here(outdir, "temp-vs-vr-smooth-1.png"), device = "png")

Finally, we need to make the \(ln(R)\) vs \(\frac{1}{2kT}\) plot similarly to Fig. 5.18 We’re going to calculate the two axis, improperly called “energy” and “lnR” (just because we need easy and short names for the code):

#Boltzmann constant
K<-8.617e-5

# X axis
energy<-1/(2*K*data$temp)

# Y axis
lnR<-log(data$Vr)

# lnR vs 1/2kT
ggplot() + geom_line(data=data.frame(x=energy,y=lnR),aes(x,y),col="blue")+
  xlab(expression(frac(1,2*k*T)))+ylab(expression(ln(R)))+
  ggtitle("ln(R) vs 1/2kT")+
  theme_tufte()

ggsave(here(outdir, "energy-gap.png"), device = "png")

To calculate the energy gap, we need to calculate the slope of the region at the start of the graph. To do that, we can fit a linear model to our data, limited to the linear region.

We’ll draw a virtual “window” between the origin of the plot and an arbitrary vertical line, we’ll call “cutoff”.

You can manually adjust the cutoff parameter to slightly before the end of the linear region with positive slope. The cutoff parameter is also printed as a blue, vertical line.

# Relative to the X axis
cutoff = 15

# Let's isolate the region of the graph were 1/2kt < cutoff
xwindow<-energy[energy<cutoff]
ywindow<-lnR[energy<cutoff]

# Create a linear fit on our windows
model.lm<-lm(ywindow~xwindow)

# Give easy name to remembers to the intercept and slope
model.lm.intercept<-model.lm$coefficients[1]
model.lm.slope<-model.lm$coefficients[2]

ggplot()+geom_line(aes(energy,lnR))+
  geom_abline(intercept=model.lm.intercept,slope=model.lm.slope,colour="red") + #print the fit line
  geom_vline(aes(xintercept = cutoff), color="blue") + #print the cutoff
  xlab(expression(frac(1,2*k*T)))+ylab(expression(ln(R)))+ # pretty print the x label
  ggtitle(expression(paste("ln(R) vs ", frac(1,2*k*T))))+
  theme_tufte() 

ggsave(here(outdir, "energy-gap-2.png"), device = "png")

We can print the calculated values with a simple “printf” like function:

cat(sprintf("The intercept is %f and the slope (Eg) is %f", model.lm.intercept, model.lm.slope))
## The intercept is -11.517888 and the slope (Eg) is 0.682384

We can create a slightly better plot by smoothing the data beforehand:

# Smooth using a spline
energyVsVres.smooth <- smooth.spline(energy,lnR)

# As before
xwindow2<-energyVsVres.smooth$x[energyVsVres.smooth$x<cutoff]
ywindow2<-energyVsVres.smooth$y[energyVsVres.smooth$x<cutoff]
model.lm2<-lm(ywindow2~xwindow2)
model.lm2.intercept<-model.lm$coefficients[1]
model.lm2.slope<-model.lm$coefficients[2]

ggplot()+geom_line(data=with(energyVsVres.smooth,data.frame(x,y)),aes(x,y))+
  geom_abline(intercept=model.lm2.intercept,slope=model.lm2.slope,colour="red")+
  geom_vline(aes(xintercept = cutoff), color="blue") + # print the cutoff
  xlab(expression(frac(1,2*k*T)))+ylab(expression(ln(R)))+
  ggtitle(expression(paste("ln(R) vs ", frac(1,2*k*T))))+
  theme_tufte() 

ggsave(here(outdir, "energy-gap-3.png"), device = "png")

and print our values calculated from the smooted graph:

cat(sprintf("The intercept is %f and the slope (Eg) is %f", model.lm2.intercept, model.lm2.slope))
## The intercept is -11.517888 and the slope (Eg) is 0.682384

8.5 Issues and questions

Any issue or question regarding the code in this handbook can be written in the repository issue page on Github.

You’re also always welcome to submit improvements sending an email to LabTrek or Fermium LABS