Storing Stuff in Memory

It’s easy, when your data is small relative to the amount of RAM you have available, to ignore how information is stored in computer memory (RAM) and on the hard drive. It’s also easy to forget that R is what’s known as a ‘high-level’ language, meaning there’s a lot of machinery in place; simple things may involve more work for the computer than is at-first apparent.

In the examples below, we’ll try storing various data types in memory and on-disk, and seeing how much space is used. See if you can figure out why these exhibit different behaviors.

Character/String Data

character_data <- data.frame(num_char = 1:200,
                             char_size = NA,
                             vec_size = NA)

for (i in 1:nrow(character_data)){
  example_vec <- rep("A", i)
  example_str <- paste(example_vec, collapse = "")
  character_data$vec_size[i] <- object.size(example_vec)
  character_data$char_size[i] <- object.size(example_str)
}


# How does the vector size grow as we add more elements?
with(character_data, plot(num_char, vec_size))
# What does this look like as a change-value?
with(character_data, plot(num_char[2:nrow(character_data)], diff(vec_size)))
abline(h = seq(0,64,8), lty = 2, col = "lightgrey")
# How does the character size grow as we add more elements?
with(character_data, plot(num_char, char_size))
# What does this look like as a change-value?
with(character_data, plot(num_char[2:nrow(character_data)], diff(char_size)))
abline(h = seq(0,64,8), lty = 2, col = "lightgrey")

Numeric Data

numeric_data <- data.frame(num_terms = 1:200,
                           num_size = NA,
                           vec_size = NA)

for (i in 1:nrow(numeric_data)){
  example_vec <- rep("1", i)
  example_num <- as.numeric(paste(example_vec, collapse = ""))
  numeric_data$vec_size[i] <- object.size(as.numeric(example_vec))
  numeric_data$num_size[i] <- object.size(example_num)
}


# How does the vector size grow as we add more elements?
with(numeric_data, plot(num_terms, vec_size))
# What does this look like as a change-value?
with(numeric_data, plot(num_terms[2:nrow(numeric_data)], diff(vec_size)))
abline(h = seq(0,64,8), lty = 2, col = "lightgrey")
# How does the size grow as we add more digits?
with(numeric_data, plot(num_terms, num_size))
# What does this look like as a change-value?
with(numeric_data, plot(num_terms[2:nrow(numeric_data)], diff(num_size)))

File Size on Disk

create_N_digit_file <- function(n, filename = "test_file.txt"){
  if (n <= 0){
    stop("What are you trying to accomplish?")
  }
  cat(paste(rep("0", n), collapse = ""), file = filename)
}


file_data <- data.frame(num_digits = 1:200,
                        file_size = NA)

for (i in 1:nrow(numeric_data)){
  create_N_digit_file(i)
  file_data$file_size[i] <- file.size("test_file.txt")
}


# How does the vector size grow as we add more elements?
with(file_data, plot(num_digits, file_size))
# What does this look like as a change-value?
with(file_data, plot(num_digits[2:nrow(numeric_data)], diff(file_size)))

Numerical Precision

Working with computer numbers is different from working with real numbers.

print(1e10 + 1000)
print(1e10)
print(1e10 + 1000 == 1e10)

# Make R show the full displayable base-10 precision
options(digits=22)
print(1e10 + 1000)

print(1e19 + 1000)
print(1e19)
print(1e19 + 1000 == 1e19)

# Working with numbers of different magnitudes is problematic
print((1e-6 + 1e10) -1e10 - 1e-6)


# Example
0.1+1e17-1e17-0.1

# Some number can't be represented exactly in base 2 at all:
0.1

# These kind of problems get MUCH worse, depending on what kind of algorithm they're embedded in:
zero <- 0
for (i in 1:1000){
  zero <- zero + 1000*((1e-6 + 1e10) -1e10 - 1e-6)
}

This is important, even for simple statistical operations.

For example, consider that, for a sample \(\{x_i, i=1,...,n\}\), we know that the sample variance has the form:

\(var({\bf x}) = \frac{1}{n-1}\sum_{i=1}^n(x_i - \bar{\bf{x}})\) \(=\frac{1}{n-1}\sum_{i=1}^n(x_i^2 - 2x_i{\bar{\bf{x}}} + {\bar{\bf{x}}}^2)\) \(=\frac{1}{n-1}\left(\sum_{i=1}^n(x_i^2) - n{\bar{\bf{x}}}^2)\right)\)

These expressions are not, however, equivalent to a computer.

variance1 <- function(x){
  (1/((length(x))-1))*sum((x-mean(x))^2)
}

variance2 <- function(x){
  ((1/((length(x))-1))*sum(x^2) - (1/((length(x))-1))*length(x)*mean(x)^2)
}

variance3 <- function(x){
  ((1/((length(x))-1))*(sum(x^2) - length(x)*mean(x)^2))
}

variance2 <- function(x){
  (1/((length(x))-1))*(sum(x^2) - length(x)*mean(x)^2)
}

dat <- rnorm(10000, mean = 1e6, sd = 0.0001)
variance1(dat)
variance2(dat)
var(dat)

dat <- rnorm(10000, mean = 1e6, sd = 1)
variance1(dat)
variance2(dat)
var(dat)