library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
mydata <- read_delim("C:\\Users\\abhir\\OneDrive\\Documents\\luddy\\H510-Stats,R\\adult.csv", delim = ",")
## Rows: 32561 Columns: 15
## āā Column specification āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
## Delimiter: ","
## chr (9): Work-Class, Education, Marital Status, Occupation, Relationship, Ra...
## dbl (6): Age, Fnl_Wgt, Education_Level, Capital_Gain, Capital_Loss, hrs_week
##
## ā¹ Use `spec()` to retrieve the full column specification for this data.
## ā¹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mydata
## # A tibble: 32,561 Ć 15
## Age `Work-Class` Fnl_Wgt Education Education_Level `Marital Status`
## <dbl> <chr> <dbl> <chr> <dbl> <chr>
## 1 39 " State-gov" 77516 " Bachelo⦠13 " Never-married"
## 2 50 " Self-emp-not-inc" 83311 " Bachelo⦠13 " Married-civ-sā¦
## 3 38 " Private" 215646 " HS-grad" 9 " Divorced"
## 4 53 " Private" 234721 " 11th" 7 " Married-civ-sā¦
## 5 28 " Private" 338409 " Bachelo⦠13 " Married-civ-sā¦
## 6 37 " Private" 284582 " Masters" 14 " Married-civ-sā¦
## 7 49 " Private" 160187 " 9th" 5 " Married-spousā¦
## 8 52 " Self-emp-not-inc" 209642 " HS-grad" 9 " Married-civ-sā¦
## 9 31 " Private" 45781 " Masters" 14 " Never-married"
## 10 42 " Private" 159449 " Bachelo⦠13 " Married-civ-sā¦
## # ā¹ 32,551 more rows
## # ā¹ 9 more variables: Occupation <chr>, Relationship <chr>, Race <chr>,
## # Sex <chr>, Capital_Gain <dbl>, Capital_Loss <dbl>, hrs_week <dbl>,
## # Country <chr>, Annual_pay <chr>
timedata <- read_delim("C:\\Users\\abhir\\Downloads\\pagesviews.csv", delim = ",")
## Rows: 366 Columns: 2
## āā Column specification āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
## Delimiter: ","
## dbl (1): Adult
## date (1): Date
##
## ā¹ Use `spec()` to retrieve the full column specification for this data.
## ā¹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
timedata
## # A tibble: 366 Ć 2
## Date Adult
## <date> <dbl>
## 1 2023-04-01 632
## 2 2023-04-02 679
## 3 2023-04-03 645
## 4 2023-04-04 618
## 5 2023-04-05 563
## 6 2023-04-06 531
## 7 2023-04-07 558
## 8 2023-04-08 461
## 9 2023-04-09 500
## 10 2023-04-10 551
## # ā¹ 356 more rows
# Load the required libraries
library(tsibble)
## Warning: package 'tsibble' was built under R version 4.3.3
##
## Attaching package: 'tsibble'
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
library(ggplot2)
# Assuming your data is stored in a dataframe named 'data'
# Convert the 'Date' column to Date format
#timedata$Date <- as.Date(timedata$Date)
#timedata$Date
# Create a tsibble object
ts_data <- tsibble(Date = timedata$Date, Adult = timedata$Adult)
## Using `Date` as index variable.
ts_data
## # A tsibble: 366 x 2 [1D]
## Date Adult
## <date> <dbl>
## 1 2023-04-01 632
## 2 2023-04-02 679
## 3 2023-04-03 645
## 4 2023-04-04 618
## 5 2023-04-05 563
## 6 2023-04-06 531
## 7 2023-04-07 558
## 8 2023-04-08 461
## 9 2023-04-09 500
## 10 2023-04-10 551
## # ā¹ 356 more rows
# Plot the data over time
ggplot(ts_data, aes(x = Date, y = Adult)) +
geom_line() +
labs(title = "Adult Count Over Time", x = "Date", y = "Adult Count") +
theme_minimal()
# Fit linear regression model
lm_model <- lm(Adult ~ Date, data = ts_data)
# Print summary of the linear regression model
summary(lm_model)
##
## Call:
## lm(formula = Adult ~ Date, data = ts_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -345.9 -61.8 -15.9 39.8 5529.2
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4094.8813 2975.9826 1.376 0.170
## Date -0.1690 0.1516 -1.115 0.266
##
## Residual standard error: 306.4 on 364 degrees of freedom
## Multiple R-squared: 0.003403, Adjusted R-squared: 0.0006648
## F-statistic: 1.243 on 1 and 364 DF, p-value: 0.2657
#Residuals: Min: The minimum residual value is -345.9. 1Q: The first quartile (25th percentile) of the residuals is -61.8. Median: The median (50th percentile) of the residuals is -15.9. 3Q: The third quartile (75th percentile) of the residuals is 39.8. Max: The maximum residual value is 5529.2.
#Coefficients: Intercept: The estimated intercept coefficient is 4094.8813 with a standard error of 2975.9826. The t-value is 1.376, and the corresponding p-value is 0.170, which suggests that the intercept coefficient is not significantly different from zero at the 0.05 significance level.
Date: The estimated coefficient for the āDateā predictor variable is -0.1690 with a standard error of 0.1516. The t-value is -1.115, and the corresponding p-value is 0.266, indicating that the coefficient is not significantly different from zero at the 0.05 significance level.
Model Fit: Residual standard error: The residual standard error is 306.4, which represents the standard deviation of the residuals around the fitted values. Multiple R-squared: The multiple R-squared value is 0.003403, indicating that only approximately 0.34% of the variance in the response variable āAdultā is explained by the predictor variable āDateā. Adjusted R-squared: The adjusted R-squared value is 0.0006648, which adjusts the R-squared value for the number of predictors in the model. F-statistic: The F-statistic tests the overall significance of the model. The F-statistic is 1.243 with a p-value of 0.2657, indicating that the overall model is not statistically significant at the 0.05 significance level.
#Conclusion: Based on the analysis:
The model does not provide a good fit to the data, as indicated by the low R-squared values and the nonsignificant coefficients. The predictor variable āDateā does not have a significant effect on the response variable āAdultā based on the coefficientsā p-values.
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
# Adjust the frequency of the time series data
ts_data <- ts(ts_data$Adult, frequency = 12) # Assuming monthly frequency
# Decompose the time series into its seasonal component
decomposed_data <- decompose(ts_data, "multiplicative")
# Plot the decomposed components
plot(decomposed_data)
# Extract decomposed components
index <- time(ts_data)
observed <- as.numeric(ts_data) # Convert ts object to numeric vector
seasonal <- decomposed_data$seasonal
trend <- decomposed_data$trend
random <- decomposed_data$random
# Create a data frame for plotting
decomposed_df <- data.frame(
index = index,
observed = observed,
seasonal = seasonal,
trend = trend,
random = random
)
# Plot the decomposed components using ggplot
ggplot(decomposed_df, aes(x = index)) +
geom_line(aes(y = observed, color = "Observed")) +
geom_line(aes(y = seasonal, color = "Seasonal")) +
geom_line(aes(y = trend, color = "Trend")) +
geom_line(aes(y = random, color = "Random")) +
scale_color_manual(values = c("Observed" = "black", "Seasonal" = "blue", "Trend" = "red", "Random" = "green")) +
labs(x = "Time", y = "Value", color = "Component") +
theme_minimal()
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
## Warning: Removed 12 rows containing missing values (`geom_line()`).
## Removed 12 rows containing missing values (`geom_line()`).
# Applying smoothing techniques (e.g., moving average)
smoothed_data <- ma(decomposed_data$seasonal, order = 12) # Applying a 12-month moving average
plot(smoothed_data)
# Plot ACF and PACF
par(mfrow = c(2, 1))
# ACF plot
acf(ts_data, main = "Autocorrelation Function (ACF)")
Positive Correlation: Peaks above the upper confidence interval indicate
positive autocorrelation, meaning that observations at the specified
lags are positively correlated. Negative Correlation: Values below the
lower confidence interval indicate negative autocorrelation, suggesting
that observations at the specified lags are negatively correlated.
At lag 0, the autocorrelation coefficient is 1, indicating perfect correlation between a data point and itself. At lag 1, the autocorrelation coefficient appears to decrease but still remains relatively high, suggesting some level of correlation between adjacent data points. At lag 2, the autocorrelation coefficient decreases further, indicating weaker correlation between data points two time units apart.
The dotted lines on the plot are confidence intervals . These lines help identify whether autocorrelation values at specific lags are statistically significant. If the autocorrelation values exceed these lines, it suggests that the correlation is unlikely to be due to random chance.
pacf(ts_data, main = "Partial Autocorrelation Function (PACF)")
This suggests that there is little to no linear relationship between
observations at that lag and the current observation.
Significance Levels: Similar to the ACF plot, the PACF plot may include horizontal lines representing the 95% and 99% confidence intervals. Values outside these lines are considered statistically significant. Lag on X-Axis: The horizontal axis represents the lag or the number of time periods between the observations being compared. Partial Correlation Coefficient on Y-Axis: The vertical axis represents the partial correlation coefficient between the observations at the specified lag, controlling for the influence of intermediate lags. This coefficient quantifies the correlation between observations after removing the effect of shorter lags. Interpretation: Peak Significance: Peaks above the confidence interval suggest significant partial autocorrelation at those lags. No Correlation: Values within the confidence interval indicate no significant partial autocorrelation at those lags. In an AR(p) model, where āpā is the order of the autoregressive component, the PACF plot typically shows significant peaks at lags up to āpā before dropping off sharply. This behavior helps in determining the appropriate order of the AR component. Seasonality Identification: Similar to the ACF plot, significant peaks at multiples of the seasonal frequency in the PACF plot can indicate the presence of seasonality.