#How to Calculate the Age of the Universe
#According to this data, the age of the universe is 13.773 billion years old. This is -0.187% off from the accepted value of 13.799 billion years old.
#Step 1: Find and Filter the Data
#Data was taken from HyperLeda, using the command: SELECT objname, mod0, vgsr WHERE mod0 IS NOT NULL, basically taking all the distance vs. velocity data they had available (4240 galaxies). After doing a quick conversion from Distance Modulus to Megaparsecs (1 Megaparsec = 3.086x10^19 km), here's what the plot looks like:
#All Data
#Hmm. This doesn't look too good. There's a huge cluster towards the origin, and a lot of sparse data and outliers on the extremes. This could really screw with our final value. Let's cut the extreme data out and just focus inside the red box I highlighted:
#Less Extreme Data
#This looks much better. The cluster is a lot more even, and there are fewer (if any) outliers. Our total galaxy count is now 3908, still 92% of our data. The data range is x<=250, y<=15000. This is the set we'll use for our calculation on the age of the universe.
#Step 2: Brush up on your Kinematics
#Find it odd that the further the galaxies are from you, the faster they're moving away from you? So did Catholic priest Georges Lemaître. Georges figured that, if you have objects moving away from you at a speed proportional to their distance from you, then abductively speaking, there must have been some kind of explosion that took place before. Take, for instance, the following diagram, which is simulated using the program in the other folder:
#Small Particle Explosion
#From this plot, you can intuitively figure two things:
#The further the particle is away from the center, the faster it's moving away from the center.
#If you were to go back in time, and tally up the speeds from each particle into the opposite direction of where they're currently moving, you'd see that they would all converge to a single point.
#Here's what it looks like when these particles are plotted as speed vs. distance:
#Small Particle Explosion
#Both these plots, by the way, would look similar if you chose any point on this plot as the center.
#Step 3: Do some simple Calculations
#As we know from physics, velocity times time equals distance (d = v*t). If we convert to a consistent set of units, divide distance (km) over velocity (km/s), we get time (s). A simple regression line works if you switch x and y (set the intercept to 0); the slope will be time in seconds. Convert into years, and, with this data, we get 13.77 billion years. That's pretty close.
#Edited to add: from another redditor:
#You may want to add that the age of the universe isn't really 1/H_0 (that's what OP computed), but there is a correction factor (that can be determined from the relative abundance of dark energy/(dark) matter/radiation) that just turns out to be very close to 1 for our universe.
#Information
#Tools: The data was compiled with R, and graphed in ggplot2.
#Source: HyperLeda, using the command: SELECT objname, mod0, vgsr WHERE mod0 IS NOT NULL
z_theme <- function() {
theme_bw(base_size=9) +
#Background and Grid formatting
theme(panel.background=element_rect(fill="#000000", color="#000000")) +
theme(plot.background=element_rect(fill="#000000", color="#000000")) +
theme(panel.border=element_rect(color="#252525")) +
theme(panel.grid.major=element_blank()) +
theme(panel.grid.minor=element_blank()) +
#Legend formatting
theme(legend.background = element_rect(fill="#000000")) +
theme(legend.text = element_blank()) +
theme(legend.title= element_blank())+
theme(legend.position="none")+
#Axis & Title Formatting
theme(plot.title=element_text(color="#D9D9D9", size=20, vjust=1.25)) +
theme(plot.subtitle=element_text(size=12,color="#BDBDBD", vjust=0)) +
theme(plot.caption=element_text(size=12,color="#BDBDBD", vjust=0)) +
theme(axis.ticks=element_blank()) +
theme(axis.text.x=element_text(size=14,color="#BDBDBD")) +
theme(axis.text.y=element_text(size=14,color="#BDBDBD")) +
theme(axis.title.x=element_text(size=16,color="#BDBDBD", vjust=0)) +
theme(axis.title.y=element_text(size=16,color="#BDBDBD", vjust=1.25))
}
#import Dataset
galaxies <- read.csv("C://Users//SAIKAT//Documents//galaxies-master//galaxies-master//galaxies.csv")
source("C://Users//SAIKAT//Documents//galaxies-master//galaxies-master//z_theme.R")
#import R Library
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
# Convert Distance Modulus to MegaParsecs
galaxies$distmpc<-10^(1+galaxies$mod0/5)/1e6
# Convert Parsecs to Kilometers
galaxies$dist<-galaxies$distmpc*3.085678e+13*1e6
# Use close galaxies for our estimation
# galaxies2<-subset(galaxies,vgsr<=15000)
galaxies2<-subset(galaxies,distmpc<=250&vgsr<=15000)
ggplot(galaxies2,aes(distmpc,vgsr))+
geom_point(shape=".",aes(color=vgsr))+
scale_color_gradientn(colours=c("white","orange","red","darkred"))+
geom_smooth(method=lm,formula=y~x+0,linetype=4,size=.5,se=F)+
# scale_x_continuous(limits=c(0,6.15e21))+
# scale_y_continuous(limits=c(-500,15000))+
labs(title="The Expanding Universe",
subtitle="The inverse of the slope of this line is equal to 13.77 billion years",
x="Distance from Earth (MPc)",
y="Velocity Away from Earth (km/s)",
caption="created by /Saikat Chowdhury")+
z_theme()

ggsave("galaxies.png",dpi=100, height=6, width=9, type="cairo-png")
# Accepted value for Age of Universe:
accage<-13.799e9 # Years
# Calculate the age of the universe:
calcage<-lm(dist~vgsr+0,data=galaxies2)$coefficients[1]/60/60/24/365.24
paste("Age of Universe:",signif(calcage,5),"years")
## [1] "Age of Universe: 1.3773e+10 years"
paste("Relative Error from accepted Age: ",
signif(100*(calcage-accage)/accage,5)
,"%",sep="")
## [1] "Relative Error from accepted Age: -0.18654%"