Module # 4 Programming structure assignment

 # ------------------------------------------------------------

# hospital_analysis.R

# Hospital patient intake dataset: plots + brief summaries

# ------------------------------------------------------------


# ---- 1) Create the dataset ----


Freq <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2)

BP   <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176)


# first: bad = 1, good = 0, NA = missing

First <- c(1, 1, 1, 1, 0, 0, 0, 0, NA, 1)


# second: low = 0, high = 1

Second <- c(0, 0, 1, 1, 0, 0, 1, 1, 1, 1)


# finaldecision: low = 0, high = 1

FinalDecision <- c(0, 1, 0, 1, 0, 1, 0, 1, 1, 1)


hospital_data <- data.frame(Freq, BP, First, Second, FinalDecision)


# Print the dataset

print("Hospital dataset:")

print(hospital_data)


# ---- 2) Boxplot: Blood Pressure by Final Decision ----

# Side-by-side comparison of BP for Low vs High final decision


boxplot(

  BP ~ FinalDecision,

  data = hospital_data,

  names = c("Low Priority (0)", "High Priority (1)"),

  main = "Blood Pressure by Final Emergency Decision",

  ylab = "Blood Pressure"

)


# ---- 3) Histogram: Blood Pressure Distribution ----


hist(

  hospital_data$BP,

  main = "Histogram of Patient Blood Pressure",

  xlab = "Blood Pressure",

  col = "lightgray",

  breaks = 5

)


# ---- 4) Quick numeric summaries (optional but helpful) ----

print("Summary of Blood Pressure:")

print(summary(hospital_data$BP))


print("BP grouped by FinalDecision (median/mean):")

print(tapply(hospital_data$BP, hospital_data$FinalDecision, median))

print(tapply(hospital_data$BP, hospital_data$FinalDecision, mean))


# ---- 5) Optional: save plots as PNG files for blog ----

# Uncomment this section if you want auto-saved images.


# png("boxplot_BP_by_FinalDecision.png", width = 900, height = 600)

# boxplot(

#   BP ~ FinalDecision,

#   data = hospital_data,

#   names = c("Low Priority (0)", "High Priority (1)"),

#   main = "Blood Pressure by Final Emergency Decision",

#   ylab = "Blood Pressure"

# )

# dev.off()

#

# png("histogram_BP.png", width = 900, height = 600)

# hist(

#   hospital_data$BP,

#   main = "Histogram of Patient Blood Pressure",

#   xlab = "Blood Pressure",

#   col = "lightgray",

#   breaks = 5

# )

# dev.off()

The histogram of blood pressure shows a wide spread among patients, with most values clustered below 120 and a few extreme high values above 170. This indicates that while most patients arrived with relatively moderate blood pressure levels, a small number presented with critically high readings.

The side-by-side boxplot comparing blood pressure by final emergency decision reveals a clear pattern. Patients categorized as high priority tend to have higher blood pressure values and greater variability compared to those classified as low priority. The median blood pressure for high-priority patients is notably higher, suggesting that blood pressure is an important factor influencing the emergency unit’s final decision.

Additionally, physician assessments appear to align with these findings. Cases involving a second doctor rating of “high” often correspond to elevated blood pressure and result in a high final decision. Overall, the results suggest that both objective measures (blood pressure) and physician evaluations play a key role in determining emergency care priority.


Comments

Popular posts from this blog

ABC vs. CBS: Comparing Fictional Presidential Poll Data in R

Assignment #10: Building Your Own R Package