¶2024-12-14
- anacoda env control
(base) alexlai@JetsonOrinNano:~/R-proj$ conda activate R-4.4
(R-4.4) alexlai@JetsonOrinNano:~/R-proj$
- Load and Preprocess Data in R
We needs these
- jsonlite
- lubridate
- dplyr
(R-4.4) alexlai@JetsonOrinNano:~/R-proj$ conda install r-jsonlite r-lubridate r-dplyr -c conda-forge
- Load data from JSON
(R-4.4) alexlai@JetsonOrinNano:~/R-proj$ R
R version 4.4.2 (2024-10-31) -- "Pile of Leaves"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: aarch64-conda-linux-gnu
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(jsonlite)
> library(lubridate)
Attaching package: ‘lubridate’
The following objects are masked from ‘package:base’:
date, intersect, setdiff, union
> 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
>
ano:~/R-proj$ ls
for example: car_in_out-2024-12-14-19:21.json
{"_id":{"$oid":"6715bdbdb64e4a4838285cb5"},"plateText":"MGW6938","inTime":"2024-09-23T15:34:09","outTime":"2024-09-23T19:32:17","carType":"motor","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbdb64e4a4838285cc2"},"plateText":"PBA0709","inTime":"2024-09-23T18:07:11","outTime":"2024-09-23T19:32:20","carType":"motor","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbdb64e4a4838285d22"},"plateText":"BND1020","inTime":"2024-09-23T08:06:51","outTime":"2024-09-23T19:01:29","carType":"car","inCameraSource":"","isExit":true,"isMonthlyCar":true,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbdb64e4a4838285d30"},"plateText":"MBQ5213","inTime":"2024-09-23T18:15:25","outTime":"2024-09-23T19:01:28","carType":"motor","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbeb64e4a4838285d63"},"plateText":"8165WE","inTime":"2024-09-23T18:06:49","outTime":"2024-09-23T18:44:48","carType":"car","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbeb64e4a4838285d9e"},"plateText":"EBF0568","inTime":"2024-09-23T17:31:48","outTime":"2024-09-23T18:25:30","carType":"car","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbeb64e4a4838285dae"},"plateText":"NUX9203","inTime":"2024-09-23T14:02:36","outTime":"2024-09-23T18:25:27","carType":"motor","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbeb64e4a4838285df1"},"plateText":"X88989","inTime":"2024-09-23T07:46:19","outTime":"2024-09-23T18:08:48","carType":"car","inCameraSource":"","isExit":true,"isMonthlyCar":true,"isUserCreate":false,"outCameraSource":""} {"_id":{"$oid":"6715bdbeb64e4a4838285e04"},"plateText":"MHL1551","inTime":"2024-09-23T14:35:17","outTime":"2024-09-23T18:08:22","carType":"motor","inCameraSource":"","isExit":true,"isMonthlyCar":false,"isUserCreate":false,"outCamer^C
(R-4.4) alexlai@JetsonOrinNano:~/R-proj$ R
R version 4.4.2 (2024-10-31) -- "Pile of Leaves"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: aarch64-conda-linux-gnu
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> data <- fromJSON("car_in_out.json")
Error in fromJSON("car_in_out.json") : could not find function "fromJSON"
> library(jsonlite)
> library(lubridate)
Attaching package: ‘lubridate’
The following objects are masked from ‘package:base’:
date, intersect, setdiff, union
> 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
> data <- fromJSON("car_in_out-2024-12-14-19:21.json")
Error in parse_con(txt, bigint_as_char) : parse error: trailing garbage
:false,"outCameraSource":""} {"_id":{"$oid":"6715b7bff679da3
(right here) ------^
> data <- fromJSON("car_in_out-12-14-24-21:23.json")
# Convert 'inTime' and 'outTime' to POSIXct datetime objects
> data$inTime <- as.POSIXct(data$inTime, format = "%Y-%m-%dT%H:%M:%S")
data$outTime <- as.POSIXct(data$outTime, format = "%Y-%m-%dT%H:%M:%S")
# Calculate parked duration in hours for each record
> data$parkedDuration <- as.numeric(difftime(data$outTime, data$inTime, units = "hours"))
# Inspect the processed data
> head(data)
3. Aggregate to Get Daily Average Parked Time
# Create a new date column based on 'inTime'
> data$date <- as.Date(data$inTime)
# Group by date and calculate the average parked duration per day
> daily_avg_parked <- data %>%
group_by(date) %>%
summarize(avgParkedTime = mean(parkedDuration, na.rm = TRUE))
# Inspect the aggregated data
print(daily_avg_parked)
4. Create a Time Series Object
Now, convert the aggregated data into a time series object.
# Create a time series object
daily_avg_ts <- ts(daily_avg_parked$avgParkedTime, start = c(year(min(daily_avg_parked$date)), month(min(daily_avg_parked$date))), frequency = 365)
# Plot the time series
plot(daily_avg_ts, main = "Daily Average Parked Time", ylab = "Average Parked Time (Hours)", xlab = "Date", col = "blue", type = "o")
5. Time Series Analysis and Visualization
You can analyze the time series further by performing decomposition, applying smoothing techniques, or forecasting future average parked times.
Example: Decomposing the Time Series
decomposed <- decompose(daily_avg_ts)
plot(decomposed)
Example: Simple Forecasting with Exponential Smoothing
library(forecast)
# Fit an exponential smoothing model
fit <- ets(daily_avg_ts)
# Forecast the next 7 days
forecasted <- forecast(fit, h = 7)
# Plot the forecast
plot(forecasted, main = "Forecasted Daily Average Parked Time")
Summary of Steps
Export your MongoDB data.
Load and preprocess it in R.
Calculate parked durations and aggregate by day.
Convert to a time series object and analyze or forecast.