# Read the DFA data.
data <- read.csv("JJG20nian.csv")$x

library(nonlinearTseries)

# Perform DFA on the data.
dfa.analysis = dfa(time.series = data, npoints = 7292,
                   window.size.range = c(60, 7292), do.plot = FALSE)

# Extract the window sizes and fluctuation function.
window_sizes <- dfa.analysis$window.sizes
fluctuation_function <- dfa.analysis$fluctuation.function

# Create a data frame.
dfa_results <- data.frame(
  Window_Size = window_sizes,
  Fluctuation_Function = fluctuation_function,
  Log_Window_Size = log10(window_sizes),
  Log_Fluctuation_Function = log10(fluctuation_function)
)

# Save as a CSV file.
write.csv(dfa_results, "E:/DFA_window_size_fluctuation.csv", row.names = FALSE)
cat("The window.size and F(t) series have been saved to: E:/DFA_window_size_fluctuation.csv\n")

