install.packages(“cluster”) library(cluster) library(vtable) library(tidyr) install.packages(‘dplyr’) library(dplyr) library(ggplot2) library(knitr) help(knitr) data<-try head(data) st(data)
names(data)
selected_vars <- data
setnames(selected_vars, old = c(“ObjectID”, “X”, “Y”, “Sector”, “Year_created”, “Artwork_Type”, “DRP_Planni”, “Population”, “No_High_Sc”, “Median_Inc”, “Transit_Ac”, “Unemployed”, “Library_Ac”, “No_Interne”, “Hispanic_L”, “White”, “Black”), new = c(“ObjectID”, “X”, “Y”, “Sector”, “Year_created”, “Artwork_Type”, “Planning_District”, “Population”, “Education_Access”, “Median_Income”, “Transit_Access”, “Unemployment”, “Library_Access”, “Internet_Access”, “Hispanic_Latino”, “White”, “Black”))
st(selected_vars) #visualizations to explore data ggplot(data, aes(x = Artwork_Type)) + geom_histogram(stat=“count”, fill = “skyblue”, color = “black”, alpha = 0.7) + labs(title = “Distribution of Artwork Types”, x = “Artwork”, y = “Frequency”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplot(data, aes(x = Median_Inc)) + geom_histogram(stat=“count”, fill = “skyblue”, color = “black”, alpha = 0.7) + labs(title = “Distribution of Median Income”, x = “Median Income”, y = “Frequency”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplot(data, aes(x = Year_created)) + geom_histogram(stat=“count”, fill = “skyblue”, color = “black”, alpha = 0.7) + labs(title = “Distribution of Artworks Created in a Year”, x = “Year”, y = “Frequency”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplot(data, aes(x = Sector)) + geom_histogram(stat=“count”, fill = “skyblue”, color = “black”, alpha = 0.7) + labs(title = “Distribution of Sectors with Artwork”, x = “Sectors”, y = “Frequency”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplot(data, aes(x = DRP_Planni)) + geom_histogram(stat=“count”, fill = “skyblue”, color = “black”, alpha = 0.7) + labs(title = “Distribution of DRP Planning Area”, x = “DRP Planning Area”, y = “Frequency”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
names(data) df <- data%>% select(ObjectID,No_High_Sc,Artwork_Type,Library_Ac,No_Interne)
numeric_columns <- sapply(df, is.numeric)
data.stand <- scale(df[, numeric_columns])
wssplot <- function(df, nc=15, seed=1234){ wss <- (nrow(df)-1)*sum(apply(df,2,var)) for (i in 2:nc){ set.seed(seed) wss[i] <- sum(kmeans(df, centers=i)$withinss)} plot(1:nc, wss, type=“b”, xlab=“Number of Clusters”, ylab=“Within groups sum of squares”)}
missing_values <- any(is.na(data.stand) | is.nan(data.stand) | is.infinite(data.stand))
if (missing_values) { # Handle missing values data.stand[is.na(data.stand) | is.nan(data.stand) | is.infinite(data.stand)] <- 0 # Replace with zeros as an example }
wssplot(data.stand, nc = 10)
k.means.fit <- kmeans(data.stand, 5) # k = 5
attributes(k.means.fit)
k.means.fit$centers
df\(Cluster <- k.means.fit\)cluster
k.means.fit$size
d <- dist(data.stand, method = “euclidean”)
points <- as.data.frame(cmdscale(d))
points\(Artwork_Type <- df\)Artwork_Type points\(Cluster <- as.factor(df\)Cluster) head(points)
library(cluster) library(ggrepel)
ggplot_obj <- ggplot(points, aes(x = V1, y = V2, color = Cluster)) + geom_point() + geom_text_repel(aes(label = Artwork_Type), size = 2, angle = 0, nudge_x = 0.05, nudge_y = 0.05) + theme_minimal() + labs(color = “Cluster”) + ggtitle(“2D Representation of the Cluster Solution”)
print(ggplot_obj)
clusplot(data.stand, df$Cluster, main=‘2D representation of the Cluster solution’, color=TRUE, shade=TRUE, labels=0, lines=0)
table(df[[1]], k.means.fit$cluster)
H.fit <- hclust(d, method=“ward.D”)
par(mar=c(1, 2, 2, 2) + 0.1)
plot(H.fit, labels = df$Artwork_Type, xlab=‘Artwork_Type’,sub =‘Dendrogram Hierarchial Relationships’,cex=0.5, las=2) # display dendogram groups <- cutree(H.fit, k=5) # cut tree into 5 clusters # draw dendogram with red borders around the 5 clusters rect.hclust(H.fit, k=5, border=“red”)
table(df[,1],groups)
#Library access based on demographic df1<-updated_table names(df1)
new <- df1%>% select(USER_Art_1,ObjectID,DRP_Planni,Hispanic_L,White,Black,Population,ami_catego) names(new)
library(data.table)
setnames(new, old = c(“USER_Art_1”, “ObjectID”, “DRP_Planni”, “Hispanic_L”, “White”, “Black”, “Population”, “ami_catego”), new = c(“Artwork_Type”, “ObjectID”, “Planning_District”, “Hispanic”, “White”, “Black”, “Population”, “Income”))
names(new)
numeric_columns <- sapply(new, is.numeric)
data.stand <- scale(new[, numeric_columns])
wssplot <- function(new, nc=15, seed=1234){ wss <- (nrow(new)-1)*sum(apply(new,2,var)) for (i in 2:nc){ set.seed(seed) wss[i] <- sum(kmeans(new, centers=i)$withinss)} plot(1:nc, wss, type=“b”, xlab=“Number of Clusters”, ylab=“Within groups sum of squares”)}
missing_values <- any(is.na(data.stand) | is.nan(data.stand) | is.infinite(data.stand))
if (missing_values) { # Handle missing values data.stand[is.na(data.stand) | is.nan(data.stand) | is.infinite(data.stand)] <- 0 # Replace with zeros as an example }
#change the margins of the plot par(mar=c(5, 8, 4, 2) + 0.1)
wssplot(data.stand, nc = 10)
k.means.fit <- kmeans(data.stand, 5) # k = 5
attributes(k.means.fit)
k.means.fit$centers
new\(Cluster <- k.means.fit\)cluster
k.means.fit$size
d <- dist(data.stand, method = “euclidean”)
points <- as.data.frame(cmdscale(d))
points\(Income <- new\)Income points\(Cluster <- as.factor(new\)Cluster) head(points)
ggplot_obj <- ggplot(points, aes(x = V1, y = V2, color = Cluster)) + geom_point() + geom_text_repel(aes(label = Income), size = 2, angle = 5, nudge_x = 0.05, nudge_y = 0.05) + theme_minimal() + labs(color = “Cluster”) + ggtitle(“2D Representation of the Cluster Solution”)
print(ggplot_obj)
clusplot(data.stand, new$Cluster, main=‘2D representation of the Cluster solution’, color=TRUE, shade=TRUE, labels=0, lines=0)
table(new[[1]], k.means.fit$cluster)
H.fit <- hclust(d, method=“ward.D”)
par(mar=c(1, 2, 2, 2) + 0.1)
plot(H.fit, labels = df$Artwork_Type, xlab=‘Artwork_Type’,sub =‘Dendrogram Hierarchial Relationships’,cex=0.5, las=2) # display dendogram groups <- cutree(H.fit, k=5) # cut tree into 5 clusters # draw dendogram with red borders around the 5 clusters rect.hclust(H.fit, k=5, border=“red”)
table(df[,1],groups)
plot(data\(Median_Inc ,data\)Library_Ac, main=‘Median Income/Library Access’, xlab=“Median Income”, ylab=“Library”,pch=20) abline(lm(data\(Library_Ac~data\)Median_Inc), col=‘red’)
ggplot(data, aes(x = Library_Ac, y = Transit_Ac)) + geom_col(fill = “black”) + # You can adjust the fill color as needed labs(title = “Library vs Transit Access”, x = “Library Access”, y = “Transit Access”) + theme_minimal()
new1<- updated_table
names(new1) summary(new1$USER_Art_1)
#create categporical values new1\(USER_Count = as.factor(new1\)USER_Count) new1\(USER_Year_ = as.factor(new1\)USER_Year_) new1\(DRP_Planni = as.factor(new1\)DRP_Planni) new1\(No_High_Sc = as.factor(new1\)No_High_Sc)
names(new1) # Now use setnames() setnames(new1, old = c(“ObjectID”,“X”,“Y”,“USER_Count”,“USER_Year_”,“USER_Art_1”, “DRP_Planni”,“Population”,“No_High_Sc”, “Transit_Ac”, “Library_Ac”, “No_Interne”, “Hispanic_L”, “White”, “Black”, “Asian”, “ami_catego”), new = c(“ObjectID”, “X”,“Y”,“Sector”,“Year_Created”,“Artwork_Type”, “Planning_District”, “Population”,“No_High_School”, “Transit_Access”, “Library_Access”, “No_Internet_Access”, “Hispanic”, “White”, “Black”, “Population”, “Income”))
names(new1) # install and load tidymodels install.packages(“tidymodels”) library(tidymodels) library(nnet)
unique(new1\(Artwork_Type) table(new1\)Artwork_Type)
new1 <- updated_table # Make sure ‘new’ has the same structure as ‘data’ new1\(Artwork_Categorized <- ifelse(new1\)Artwork_Type %in% c(“Mural”, “Sculpture”, “Painting”), new1$Artwork_Type, “Other”) multinom_model <- multinom(Artwork_Categorized ~ Library_Access + No_High_School + No_Internet_Access, data = new1)
summary(multinom_model)
library(stargazer) #export stargazer table to txt file stargazer(multinom_model, type = “text”, title = “Logistic Regression Results”, out = “final.txt”)
predicted_probs <- predict(multinom_model, newdata = new1, type = “probs”) new1\(pred_class <- apply(predicted_probs, 1, function(x) names(which.max(x))) conf_matrix <- table(Predicted = new1\)pred_class, Actual = new1$Artwork_Categorized) print(conf_matrix)
accuracy <- sum(diag(conf_matrix)) / sum(conf_matrix) print(paste(“Accuracy:”, round(accuracy, 2)))
library(nnet) # for multinom() library(ggplot2) # for ggplot()
coefs <- coef(multinom_model)
exp_coefs <- exp(coefs) percent_change <- (exp_coefs - 1) * 100
coefs_df <- as.data.frame(t(coefs)) coefs_df$Feature <- rownames(coefs_df) coefs_long_df <- reshape2::melt(coefs_df, id.vars = “Feature”)
ggplot(coefs_long_df, aes(x = value, y = Feature)) + geom_col() + labs(title = “Percent Change of Odds for Each Feature”, x = “Percent Change”, y = “Feature”) + theme_minimal()