# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R
#     language: R
#     name: ir
# ---

# # High Energy Physics example

tracks <- read.csv("data/tracks_real_sorted.csv")

dim(tracks)

tracks <- tracks[,2:175]

tracks_pt <- sqrt(tracks[,4]^2+tracks[,5]^2)

tracks <- tracks[,7:174]

dim(tracks)

168/6

counter <- 1;
for(i in seq(1,168, by=6)){
    names(tracks)[i] <-  paste("X", counter, sep="");
    names(tracks)[i+1] <-  paste("Y", counter, sep="");
    names(tracks)[i+2] <-  paste("Z", counter, sep="");
    names(tracks)[i+3] <-  paste("CH0_", counter, sep="");
    names(tracks)[i+4] <-  paste("CH1_", counter, sep="");
    names(tracks)[i+5] <-  paste("W", counter, sep="");
    counter <- counter +1;
}

names(tracks)

new_tracks <- data.frame(0)
counter <- 1;
for(i in seq(1,168, by=6)){
    new_tracks <- cbind(new_tracks, tracks[,i])
    new_tracks <- cbind(new_tracks, tracks[,i+1])
    new_tracks <- cbind(new_tracks, tracks[,i+2])
    counter <- counter +1;
}

dim(new_tracks)

tracks <- new_tracks[,2:85]

dim(tracks)

head(tracks)

names(tracks)

counter <- 1;
for(i in seq(1,84, by=3)){
    names(tracks)[i] <-  paste("X", counter, sep="");
    names(tracks)[i+1] <-  paste("Y", counter, sep="");
    names(tracks)[i+2] <-  paste("Z", counter, sep="");
    counter <- counter +1;
}

tracks <- cbind(tracks, tracks_pt)

names(tracks)[85] <- "pt"

dim(tracks)

model <- lm(tracks$pt ~ ., data=tracks)

summary(model)

set.seed(123)

nrow(tracks)

sample_size <- as.integer(nrow(tracks)*.8)

sample_index <- sample(nrow(tracks), sample_size, replace = FALSE)

tracks_train <- tracks[sample_index,]

tracks_test <- tracks[-sample_index,]

plot(tracks_test$X1, tracks_test$pt)

tracks.lm <- lm(pt ~ poly(X1,2)+ poly(Y1,2) , data=tracks_train)

summary(tracks.lm)

predictions <- predict(tracks.lm, data = tracks_test[1:84])

sum((predictions-tracks_test$pt)^2)

rmse <- sqrt(mean((tracks_test$pt - predictions)^2))

barplot(tracks_test$pt[tracks_test$pt < 5])

barplot(predictions)

rmse

write.csv(tracks, "data/tracks.csv",  row.names=FALSE)

tr <- read.csv("data/tracks.csv")

names(tr)

dim(tr)




StarcoderdataJupyterScriptsDedupFiltered