From b5e715400e2a99fc7fdbd97357a102bfef73b98a Mon Sep 17 00:00:00 2001 From: Alex Gebben Work Date: Mon, 6 Oct 2025 17:09:22 -0600 Subject: [PATCH] Added working note --- ARMA_Pop.r | 6 +- Scripts/Data_Load.r | 130 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 Scripts/Data_Load.r diff --git a/ARMA_Pop.r b/ARMA_Pop.r index bcad0c8..64d873e 100644 --- a/ARMA_Pop.r +++ b/ARMA_Pop.r @@ -1,3 +1,5 @@ +######################################DATA WORKING OFF OF DATA SCRIPT!!!!!!!!!!!!!!!!!!! Start there + #Future things to consider ###Naughton 1 & 2 natural gas conversion ###Add some variance in employment at TerraPower (odds of more or less) @@ -26,7 +28,7 @@ plot(ARMA_POP,main="Lincoln County Population Forecast",xlab="Year",ylab="Popula DIAMOND <- c(1000,1070,1114,1101,1082,1078,1063,991,916,876,864,863,847,835,835,827,808,774,748,732,705,695,690,689,695,700,710,723,738,745,731,704,677,667,652,629,613,586,559,540,523,526,527,521,517) AREA_POP <- KEM+DIAMOND LN <- c(12177,13254,14031,14110,14111,14319,14384,13658,12875,12552,12625,12975,13124,13329,13759,14073,14206,14099,14114,14338,14621,14697,14858,15117,15539,15917,16429,17013,17629,18082,18083,17946,17822,18148,18346,18473,18766,18899,19042,19379,19658,20174,20690,20909,21000) - NO_CITY <- c(10095,10392,10747,10944,11043) +# NO_CITY <- c(10095,10392,10747,10944,11043) YEAR <- 1980:2024 ####Old data addtion:Period Ends in 1970 #See in part http://eadiv.state.wy.us/demog_data/cntycity_hist.htm @@ -40,7 +42,7 @@ plot(ARMA_POP,main="Lincoln County Population Forecast",xlab="Year",ylab="Popula A <- cbind(YEAR2,LN2) %>% as_tibble %>% rename(Population=LN2) %>% mutate(Region='Lincoln County') B <- cbind(YEAR2,AREA2) %>% as_tibble %>% rename(Population=AREA2) %>% mutate(Region='Kemmerer & Diamondvile') DATA <- rbind(A,B) %>% rename(Year=YEAR2) - ggplot(aes(x=Year,y=Population,group=Region,color=Region),data=DATA2) +geom_line(linewidth=1.5) + ggplot(aes(x=Year,y=Population,group=Region,color=Region),data=DATA) +geom_line(linewidth=1.5)+scale_x_continuous()+geom_vline(xintercept= 2022, linetype = "dashed", color = "red",size = 1) ###Kemmerer ARMA KEM_TS <- DATA %>% filter(Year>=1980,Region=='Kemmerer & Diamondvile') %>% pull(Population) %>% ts(start=c(1980),end=c(2024),frequency=1) BC <- BoxCox.lambda(KEM_TS) diff --git a/Scripts/Data_Load.r b/Scripts/Data_Load.r new file mode 100644 index 0000000..af2147d --- /dev/null +++ b/Scripts/Data_Load.r @@ -0,0 +1,130 @@ +library(rvest) +library(tidyverse) + + +########County, Death, Birth and Migration Data +#Data found on the page http://eadiv.state.wy.us/pop/ +PAGE <- read_html("http://eadiv.state.wy.us/pop/BirthDeathMig.htm") +NODE <- html_element(PAGE ,"table") +TBL <- html_table(NODE) + +ST <- which(toupper(TBL$X1)=="ALBANY") +END <- which(toupper(TBL$X1)=="TOTAL") +TYPES <- TBL[ST-2,1] + +ST_YEAR <- 1971 +ALL_DATA <- list() +TBL <- TBL[,c(1,which(!is.na(as.numeric(TBL[ST[1],]))))] +TBL <- TBL[,-ncol(TBL)] +colnames(TBL) <- c("County",(ST_YEAR:(ST_YEAR+ncol(TBL)-1))) +TBL$Type <- NA +for(i in 1:length(ST)){ + TBL[ST[i]:END[i],"Type"]<- as.character(TYPES[i,1]) +} +TBL[ST[2]:END[2],"Type"] <- as.character(TYPES[2,1]) +TBL$Type +TBL <- TBL %>% filter(!is.na(Type)) %>% select(County,Type,everything()) +GROUP <- colnames(TBL)[-1:-2] +Data <- pivot_longer(TBL,all_of(GROUP),names_to="Year",values_to="Pop_Change") +Data$County <- ifelse(toupper(Data$County)=="TOTAL","Wyoming",Data$County) +WY_COUNTY_DATA_SET <- pivot_wider(Data,names_from=Type,values_from=Pop_Change) %>% rename("Migration"=`Net Migration`) %>% mutate(Year=as.integer(Year),Births=parse_number(Births),Deaths=parse_number(Deaths),Migration=parse_number(Migration)) + +########################City and County Population Data 2020 to 2024 +PAGE <- read_html('http://eadiv.state.wy.us/pop/Place-24EST.htm') +NODE <- html_element(PAGE ,"table") +TBL <- html_table(NODE) + +ST <- which(toupper(TBL$X1)==toupper("Albany County")) +END <- which(toupper(TBL$X1)==toupper("Balance of Weston County")) + +#More years than are pulled are listed to make more generic +COLUMNS <- c(1,which(TBL[ST-2,] %in% 1970:2025)) +NAMES <- TBL[4,COLUMNS][-1] +TBL <- TBL[ST:END,COLUMNS ] +colnames(TBL) <- c("County",NAMES) + +TBL <- pivot_longer(TBL,all_of(colnames(TBL)[-1]),names_to="Year",values_to="Population") %>% mutate(Year=as.integer(Year),Population=parse_number(Population)) +TBL$County <- gsub(" "," ",gsub("\n","",gsub("\r","",TBL %>% pull(County)))) + +COUNTY_POP<- TBL[grep("COUNTY",TBL %>% pull(County),ignore.case=TRUE),] +COUNTY_POP<- COUNTY_POP[grep("Balance",COUNTY_POP%>% pull(County),invert=TRUE,ignore.case=TRUE),] +COUNTY_POP$County <- gsub(" ","_",gsub(" County","",COUNTY_POP$County)) +CITY_POP <- TBL[sort(c(grep("County",TBL %>% pull(County),invert=TRUE,ignore.case=TRUE),grep("Balance",TBL %>% pull(County),ignore.case=TRUE))),] +CITY_POP$County <- gsub(" ","_",gsub("Balance of","Unincorporated",gsub(" County","",gsub(" city","",gsub(" town","",CITY_POP$County,ignore.case=TRUE),ignore.case=TRUE),ignore.case=TRUE),ignore.case=TRUE)) +CITY_POP <- CITY_POP %>% rename("City"=County) +########################City Population Data 2010 to 2020 +PAGE <- read_html('http://eadiv.state.wy.us/pop/sub-est11-19.htm') +NODE <- html_element(PAGE ,"table") +TBL <- html_table(NODE) +ST <- which(toupper(TBL$X1)==toupper("Afton town, Wyoming")) +END <- which(toupper(TBL$X1)==toupper("Yoder town, Wyoming")) +#More years than are pulled are listed to make more generic +COLUMNS <- c(1,which(TBL[ST-1,] %in% 1970:2025)) +NAMES <- TBL[3,COLUMNS][-1] +TBL <- TBL[ST:END,COLUMNS ] +colnames(TBL) <- c("City",NAMES) +TBL <- pivot_longer(TBL,all_of(colnames(TBL)[-1]),names_to="Year",values_to="Population") %>% mutate(Year=as.integer(Year),Population=parse_number(Population)) +TBL$City <- gsub(" ","_",gsub(" $","",gsub("\r|\n| Wyoming|,| town| city","",TBL$City,ignore.case=TRUE))) +CITY_POP <- rbind(TBL,CITY_POP) +########################County Population Data 2010 to 2020 +PAGE <- read_html('http://eadiv.state.wy.us/pop/ctyest11-19.htm') +NODE <- html_element(PAGE ,"table") +TBL <- html_table(NODE) +ST <- grep("Albany",TBL$X1) +END <- grep("Weston",TBL$X1) +#More years than are pulled are listed to make more generic +COLUMNS <- c(1,which(TBL[ST-2,] %in% 1970:2025)) +NAMES <- TBL[3,COLUMNS][-1] +TBL <- TBL[ST:END,COLUMNS ] +colnames(TBL) <- c("County",NAMES) +TBL <- pivot_longer(TBL,all_of(colnames(TBL)[-1]),names_to="Year",values_to="Population") %>% mutate(Year=as.integer(Year),Population=parse_number(Population)) +TBL$County <- gsub(" ","_",gsub(" "," ",gsub(" $","",gsub("\r|\n| Wyoming|,| town| city| County|\\.","",TBL$County,ignore.case=TRUE)))) + +COUNTY_POP <- rbind(TBL,COUNTY_POP) + +########################County and City Population Data 2000 to 2010 +PAGE <- read_html('http://eadiv.state.wy.us/pop/sub-est01-09.htm') +NODE <- html_element(PAGE ,"table") +TBL <- html_table(NODE) + +ST <- which(toupper(TBL$X1)==toupper("Albany County")) +END <- which(toupper(TBL$X1)==toupper("Balance of Weston County")) + +#More years than are pulled are listed to make more generic +COLUMNS <- c(1,which(TBL[ST-4,] %in% 1970:2025)) +NAMES <- TBL[4,COLUMNS][-1] +TBL <- TBL[ST:END,COLUMNS ] +colnames(TBL) <- c("County",NAMES) +TBL <- pivot_longer(TBL,all_of(colnames(TBL)[-1]),names_to="Year",values_to="Population") %>% mutate(Year=as.integer(Year),Population=parse_number(Population)) +TBL$County <- gsub(" "," ",gsub("\n","",gsub("\r","",TBL %>% pull(County)))) + +COUNTY_TBL <- TBL[grep("COUNTY",TBL %>% pull(County),ignore.case=TRUE),] +COUNTY_TBL <-COUNTY_TBL[grep("Balance",COUNTY_TBL%>% pull(County),invert=TRUE,ignore.case=TRUE),] +COUNTY_TBL$County <-gsub("_(pt.)","", gsub(" ","_",gsub(" County","",COUNTY_TBL$County))) + +CITY_TBL <- TBL[sort(c(grep("County",TBL %>% pull(County),invert=TRUE,ignore.case=TRUE),grep("Balance",TBL %>% pull(County),ignore.case=TRUE))),] +CITY_TBL$County <- gsub(" ","_",gsub("Balance of","Unincorporated",gsub(" County","",gsub(" city","",gsub(" town","",CITY_TBL$County,ignore.case=TRUE),ignore.case=TRUE),ignore.case=TRUE),ignore.case=TRUE)) +CITY_TBL <- CITY_TBL %>% rename("City"=County) +CITY_POP <- rbind(CITY_TBL,CITY_POP) +#Cleanup names +CITY_POP$City <- gsub("LaGrange","La_Grange",CITY_POP$City) +COUNTY_POP <- rbind(COUNTY_TBL,COUNTY_POP) +####################County and City Population DAta for 1990-2000 +PAGE <- read_html('http://eadiv.state.wy.us/pop/c&sc90_00.htm') +NODE <- html_element(PAGE ,"body") +TEST <- read_table(html_text2(NODE),skip=21,skip_empty_rows=TRUE) +TO_ADJ <- sum((!is.na(TEST[2,])))-11 + +?read_table +?html_text2 +?html_text +TBL <- html_table(NODE) +TBL + +ST <- which(toupper(TBL$X1)==toupper("Albany Cnty")) +END <- which(toupper(TBL$X1)==toupper("Balance of Weston County")) + + + + +