# Question 1 - Creating the given matrix in RStudio
# (1) Logging data into vectors
patient.info <- c("Patient 1", "Patient 1", "Patient 1", "Patient 2", "Patient 2", "Patient 2", "Patient 3", "Patient 3", "Patient 3", "Patient 4", "Patient 4", "Patient 4", "Patient 5", "Patient 5", "Patient 5")
visit.info <- c("1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3")
age.info <- c("11", "11", "11", "20", "20", "20", "64", "64", "64", "45", "45", "45", "31", "31", "31")
weight.info <- c("110", "112", "115", "170", "175", "178", "145", "150", "148", "215", "220", "225", "180", "185", "188")
drugA.dose.info <- c("75", "76", "77", "115", "118", "120", "100", "102", "104", "145", "147", "150", "120", "123", "125")
drugB.dose.info <- c("30", "31", "32", "50", "52", "53", "40", "41", "42", "60", "62", "63", "45", "46", "47")
# (2) Creating and displaying matrix
nursing.department <- cbind(patient.info, visit.info, age.info, weight.info, drugA.dose.info, drugB.dose.info)
rownames(nursing.department) <- c("","","","","","","","","","","","","","","")
colnames(nursing.department) <- c("Patient", "Visit", "Age", "Weight", "Drug A Dose", "Drug B Dose")
nursing.department
## Patient Visit Age Weight Drug A Dose Drug B Dose
## "Patient 1" "1" "11" "110" "75" "30"
## "Patient 1" "2" "11" "112" "76" "31"
## "Patient 1" "3" "11" "115" "77" "32"
## "Patient 2" "1" "20" "170" "115" "50"
## "Patient 2" "2" "20" "175" "118" "52"
## "Patient 2" "3" "20" "178" "120" "53"
## "Patient 3" "1" "64" "145" "100" "40"
## "Patient 3" "2" "64" "150" "102" "41"
## "Patient 3" "3" "64" "148" "104" "42"
## "Patient 4" "1" "45" "215" "145" "60"
## "Patient 4" "2" "45" "220" "147" "62"
## "Patient 4" "3" "45" "225" "150" "63"
## "Patient 5" "1" "31" "180" "120" "45"
## "Patient 5" "2" "31" "185" "123" "46"
## "Patient 5" "3" "31" "188" "125" "47"
# Question 2 - Creating a smaller matrix
# (1) Logging mini data into vectors
mini.patient.info <- c("Patient 1", "Patient 1", "Patient 1", "Patient 2", "Patient 2", "Patient 2", "Patient 4", "Patient 4", "Patient 4")
mini.visit.info <- c("1", "2", "3", "1", "2", "3", "1", "2", "3")
mini.age.info <- c("11", "11", "11", "20", "20", "20", "45", "45", "45")
mini.weight.info <- c("110", "112", "115", "170", "175", "178", "215", "220", "225")
mini.drugA.dose.info <- c("75", "76", "77", "115", "118", "120", "145", "147", "150")
mini.drugB.dose.info <- c("30", "31", "32", "50", "52", "53", "60", "62", "63")
# (2) Creating and displaying mini matrix
mini.nursing.department <- cbind(mini.patient.info, mini.visit.info, mini.age.info, mini.weight.info, mini.drugA.dose.info, mini.drugB.dose.info)
rownames(mini.nursing.department) <- c("","","","","","","","","")
colnames(mini.nursing.department) <- c("Patient", "Visit", "Age", "Weight", "Drug A Dose", "Drug B Dose")
mini.nursing.department
## Patient Visit Age Weight Drug A Dose Drug B Dose
## "Patient 1" "1" "11" "110" "75" "30"
## "Patient 1" "2" "11" "112" "76" "31"
## "Patient 1" "3" "11" "115" "77" "32"
## "Patient 2" "1" "20" "170" "115" "50"
## "Patient 2" "2" "20" "175" "118" "52"
## "Patient 2" "3" "20" "178" "120" "53"
## "Patient 4" "1" "45" "215" "145" "60"
## "Patient 4" "2" "45" "220" "147" "62"
## "Patient 4" "3" "45" "225" "150" "63"
Key Differences:
# Question 3 - Range, mean and standard deviation of all Drug A doses filtered through each visit
# (1) Logging data into new vectors
visit1.drugA.dose.info <- c("75", "115", "100", "145", "120")
visit2.drugA.dose.info <- c("76", "118", "102", "147", "123")
visit3.drugA.dose.info <- c("77", "120", "104", "150", "125")
# (2) Calculating range, mean and standard deviation of Drug A doses for each visit
visit1.range <- diff(range(as.numeric(visit1.drugA.dose.info)))
visit1.mean <- mean(as.numeric(visit1.drugA.dose.info))
visit1.sd <- sd(as.numeric(visit1.drugA.dose.info))
visit2.range <- diff(range(as.numeric(visit2.drugA.dose.info)))
visit2.mean <- mean(as.numeric(visit2.drugA.dose.info))
visit2.sd <- sd(as.numeric(visit2.drugA.dose.info))
visit3.range <- diff(range(as.numeric(visit3.drugA.dose.info)))
visit3.mean <- mean(as.numeric(visit3.drugA.dose.info))
visit3.sd <- sd(as.numeric(visit3.drugA.dose.info))
# (3) Displaying results
cat("Visit 1 Range: ", visit1.range, "\n")
## Visit 1 Range: 70
cat("Visit 1 Mean: ", visit1.mean, "\n")
## Visit 1 Mean: 111
cat("Visit 1 Standard Deviation: ", visit1.sd, "\n")
## Visit 1 Standard Deviation: 25.83602
cat("Visit 2 Range: ", visit2.range, "\n")
## Visit 2 Range: 71
cat("Visit 2 Mean: ", visit2.mean, "\n")
## Visit 2 Mean: 113.2
cat("Visit 2 Standard Deviation: ", visit2.sd, "\n")
## Visit 2 Standard Deviation: 26.31919
cat("Visit 3 Range: ", visit3.range, "\n")
## Visit 3 Range: 73
cat("Visit 3 Mean: ", visit3.mean, "\n")
## Visit 3 Mean: 115.2
cat("Visit 3 Standard Deviation: ", visit3.sd, "\n")
## Visit 3 Standard Deviation: 26.99444
# (4) Plotting results by range, mean and standard deviation
visit.range <- c(visit1.range, visit2.range, visit3.range)
visit.mean <- c(visit1.mean, visit2.mean, visit3.mean)
visit.sd <- c(visit1.sd, visit2.sd, visit3.sd)
plot(visit.range, , xlab = "Visit", ylab = "Frequency", main = "Range of Drug A Doses")
plot(visit.mean, , xlab = "VIsit", ylab = "Frequency", main = "Mean of Drug A Doses")
plot(visit.sd, , xlab = "Visit", ylab = "Frequency", main = "Standard Deviation of Drug A Doses")
Question 3 summary
Summary of Observations Across the three visits, both the mean and range of Drug A doses show a slight upward trend, reflecting an overall increase in doses administered over time. The standard deviation remains consistent across visits, suggesting that the variability in doses between patients does not fluctuate significantly over the course of treatment. The plotted results of the range, mean, and standard deviation visually reinforce these patterns, highlighting the gradual increase in drug dosage over time while maintaining relatively stable variability among patients. This consistency suggests a controlled adjustment in drug administration practices across visits.
# Question 4 - Scatter plot of age vs. each visit's dose A + dose B per age. Box plot of each drug dosage separately.
# (1.1) Logging data into new vectors as numeric
visit1.drugA.dose.info <- c(75, 115, 100, 145, 120)
visit1.drugB.dose.info <- c(30, 50, 40, 60, 45)
visit2.drugA.dose.info <- c(76, 118, 102, 147, 123)
visit2.drugB.dose.info <- c(31, 52, 41, 62, 46)
visit3.drugA.dose.info <- c(77, 120, 104, 150, 125)
visit3.drugB.dose.info <- c(32, 53, 42, 63, 47)
age.info <- c(11, 20, 64, 45, 31)
# (1.2) Adding respective drug doses for each visit
visit1.total.dose <- visit1.drugA.dose.info + visit1.drugB.dose.info
visit2.total.dose <- visit2.drugA.dose.info + visit2.drugB.dose.info
visit3.total.dose <- visit3.drugA.dose.info + visit3.drugB.dose.info
total.dose <- c(visit1.total.dose, visit2.total.dose, visit3.total.dose)
# (1.3) Repeating age information to match total dose length
age.repeated <- rep(age.info, 3)
# (1.4) Creating scatter plot of age vs. total dose for each age
plot(age.repeated, total.dose, xlab = "Age", ylab = "Total Dose", main = "Age vs. Total Dose")
# (2.1) Creating box plot of both drug dosage
boxplot(visit1.drugA.dose.info, visit1.drugB.dose.info, visit2.drugA.dose.info, visit2.drugB.dose.info, visit3.drugA.dose.info, visit3.drugB.dose.info, names = c("Visit 1 Drug A", "Visit 1 Drug B", "Visit 2 Drug A", "Visit 2 Drug B", "Visit 3 Drug A", "Visit 3 Drug B"), xlab = "Visit", ylab = "Dose", main = "Drug Dosage Box Plot")
Question 4 summary
Age vs. Total Dose:
This scatter plot illustrates the relationship between patient age and the total dose administered (the sum of Drug A and Drug B doses). Younger patients (ages 11 and 20) consistently receive lower total doses, clustered around 120–160. In contrast, patients aged 45 receive notably higher total doses, around 200. This suggests a trend where older patients are administered higher doses. However, the relationship between age and total dose does not appear to be strictly linear, as there is a noticeable gap in the age distribution (no data for patients in their 30s or 50s).
Drug Dosage Box Plot:
This box plot compares the dosage distributions for Drug A and Drug B across three visits. Drug A consistently shows higher dosage levels compared to Drug B, with a wider range of doses. For example, during Visit 1, Drug A doses range between 70 and 140, while Drug B doses remain between 30 and 60. Across the three visits, there is a slight increase in the median dosage for both drugs, indicating that dosage is adjusted upward over time. The variance in Drug A dosages is more pronounced across visits, whereas Drug B shows a more consistent distribution across all three visits. These observations point to a pattern of increasing dosages over time, with Drug A being administered at higher and more variable levels compared to Drug B.