top of page
Joz Art Logo24=portrait.png

R is one of the most powerful tools used in data analysis and statistical modeling. This short guide, demonstrates how to analyze and visualize quantitative data in R.

It's a time-series analysis given the changes in carbon dioxide over time.


Sample Question

Visualize with ggplot in an appropriate way the evolution of carbon dioxide content (the variable co2_ppm) during the entire period. Use the decimal_date variable as the time. Also, enter appropriate chart and axis headings.


The dataset is in two columns, year (1959 - 2023) and Growth (co2_ppm value corresponding to each year)


1959 0.94 0.11 1960 0.50 0.11 1961 0.96 0.11 1962 0.64 0.11 1963 0.71 0.11 1964 0.32 0.11 1965 1.06 0.11 1966 1.28 0.11 1967 0.70 0.11 1968 1.06 0.11 1969 1.35 0.11 1970 1.00 0.11 1971 0.81 0.11 1972 1.74 0.11 1973 1.18 0.11 1974 0.95 0.11 1975 1.09 0.11 1976 0.79 0.11 1977 2.15 0.11 1978 1.30 0.11 1979 1.83 0.11 1980 1.68 0.11 1981 1.43 0.11 1982 0.86 0.11 1983 2.36 0.11 1984 1.51 0.11 1985 1.21 0.11 1986 1.47 0.11 1987 2.06 0.11 1988 2.24 0.11 1989 1.24 0.11 1990 1.20 0.11 1991 1.05 0.11 1992 0.49 0.11 1993 1.36 0.11 1994 1.95 0.11 1995 2.01 0.11 1996 1.24 0.11 1997 1.91 0.11 1998 2.97 0.11 1999 0.92 0.11 2000 1.62 0.11 2001 1.62 0.11 2002 2.51 0.11 2003 2.27 0.11 2004 1.59 0.11 2005 2.57 0.11 2006 1.69 0.11 2007 2.31 0.11 2008 1.54 0.11 2009 2.00 0.11 2010 2.30 0.11 2011 1.92 0.11 2012 2.65 0.11 2013 1.99 0.11 2014 2.17 0.11 2015 2.95 0.11 2016 3.03 0.11 2017 1.89 0.11 2018 2.85 0.11 2019 2.49 0.11 2020 2.27 0.11 2021 2.37 0.11 2022 1.81 0.11 2023 3.36 0.11


Solution

Start by loading the required library:

library(ggplot2)


In R, set the dataframe as follows:

data <- data.frame(

  Year = 1959:2023,

  Growth = c(

    0.94, 0.50, 0.96, 0.64, 0.71, 0.32, 1.06, 1.28, 0.70, 1.06,

    1.35, 1.00, 0.81, 1.74, 1.18, 0.95, 1.09, 0.79, 2.15, 1.30,

    1.83, 1.68, 1.43, 0.86, 2.36, 1.51, 1.21, 1.47, 2.06, 2.24,

    1.24, 1.20, 1.05, 0.49, 1.36, 1.95, 2.01, 1.24, 1.91, 2.97,

    0.92, 1.62, 1.62, 2.51, 2.27, 1.59, 2.57, 1.69, 2.31, 1.54,

    2.00, 2.30, 1.92, 2.65, 1.99, 2.17, 2.95, 3.03, 1.89, 2.85,

    2.49, 2.27, 2.37, 1.81, 3.36

  )

)


Next, create the plot

ggplot(data = data, aes(x = Year, y = Growth)) +

  geom_line() +

  labs(title = "Evolution of Carbon Dioxide Content Over Time",

       x = "Year",

       y = "Growth (ppm)")


The plot should look like this based on the given data:






How to run EFA and CFA analysis in R

Here are the basic steps for running EFA and CFA in R using the psych and lavaan packages, respectively.


Exploratory Factor Analysis (EFA) using the psych package:

Sample EFA Output


Firstly, make sure you have the necessary packages installed. You can install them by running:

install.packages("psych")

install.packages("lavaan")

EFA Example:

# Load the required library library(psych)

# Example dataset (replace with your own dataset) data <- read.csv("your_data.csv")

# Run EFA efa_result <- fa(data, nfactors = 3, rotate = "varimax")  

# Change nfactors according to your analysis

# View the factor loadings print(efa_result$loadings)


Confirmatory Factor Analysis (CFA) using the lavaan package:

# Load the required library library(lavaan)

# Example dataset (replace with your own dataset) data <- read.csv("your_data.csv")

# Define the CFA model cfa_model <- ' # Define your model here using syntax from lavaan latent_variable =~ observed_item1 + observed_item2 + observed_item3 # Add more variables and relationships as needed '

# Fit the CFA model cfa_result <- lavaan::cfa(cfa_model, data = data, estimator = "ML")  

# Change estimator as needed

# Summarize the results summary(cfa_result, fit.measures = TRUE)


bottom of page