Understanding restriction of range with Shiny!
I made this: https://emilkirkegaard.shinyapps.io/Understanding_restriction_of_range/
Source:
# ui.R
shinyUI(fluidPage(
titlePanel(title, windowTitle = title),
sidebarLayout(
sidebarPanel(
helpText("Get an intuitive understanding of restriction of range using this interactive plot. The slider below limits the dataset to those within the limits."),
sliderInput("limits",
label = "Restriction of range",
min = -5, max = 5, value = c(-5, 5), step=.1),
helpText("Note that these are Z-values. A Z-value of +/- 2 corresponds to the 98th or 2th centile, respectively.")
),
mainPanel(
plotOutput("plot"),width=8,
textOutput("text")
)
)
))
# server.R
shinyServer(
function(input, output) {
output$plot <- renderPlot({
#limits
lower.limit = input$limits[1] #lower limit
upper.limit = input$limits[2] #upper limit
#adjust data object
data["X.restricted"] = data["X"] #copy X
data[data[,1]<lower.limit | data[,1]>upper.limit,"X.restricted"] = NA #remove values
group = data.frame(rep("Included",nrow(data))) #create group var
colnames(group) = "group" #rename
levels(group$group) = c("Included","Excluded") #add second factor level
group[is.na(data["X.restricted"])] = "Excluded" #is NA?
data["group"] = group #add to data
#plot
xyplot(Y ~ X, data, type=c("p","r"), col.line = "darkorange", lwd = 1,
group=group, auto.key = TRUE)
})
output$text <- renderPrint({
#limits
lower.limit = input$limits[1] #lower limit
upper.limit = input$limits[2] #upper limit
#adjust data object
data["X.restricted"] = data["X"] #copy X
data[data[,1]<lower.limit | data[,1]>upper.limit,"X.restricted"] = NA #remove values
group = data.frame(rep("Included",nrow(data))) #create group var
colnames(group) = "group" #rename
levels(group$group) = c("Included","Excluded") #add second factor level
group[is.na(data["X.restricted"])] = "Excluded" #is NA?
data["group"] = group #add to data
#correlations
cors = cor(data[1:3], use="pairwise")
r = round(cors[3,2],2)
#print output
str = paste0("The correlation in the full dataset is .50, the correlation in the restricted dataset is ",r)
print(str)
})
}
)
#global.R
library("lattice")
data = read.csv("data.csv",row.names = 1) #load data
title = "Understanding restriction of range"