NOAA-OCM / SWMPrExtension

Functions for Analyzing and Plotting SWMP Estuary Monitoring Data from the NERR System archive at
http://cdmo.baruch.sc.edu/
Other
12 stars 5 forks source link

lm_p_labs #27

Closed padilla410 closed 6 years ago

padilla410 commented 6 years ago

Right now it fails when a season is missing `my_lm_p_labs <- function(dat_in) {

remove seasons with out results

dat_in <- dat_in[complete.cases(dat_in), ]

lm_results <- dat_in %>% group_by(.data$season) %>% do(reg_min = lm(.data$min ~ .data$year, data = .data, na.action = 'na.omit') , reg_mean = lm(.data$mean ~ .data$year, data = .data, na.action = 'na.omit') , reg_max = lm(.data$max ~ .data$year), data = .data, na.action = 'na.omit')

lm_min_tidy <- tidy(lm_results, reg_min) %>% filter(.data$term == '.data$year') lm_mean_tidy <- tidy(lm_results, reg_mean) %>% filter(.data$term == '.data$year') lm_max_tidy <- tidy(lm_results, reg_max) %>% filter(.data$term == '.data$year')

lm_min_tidy$lab <- ifelse(lm_min_tidy$p.value < 0.05, 'p < 0.05', 'p > 0.05') lm_mean_tidy$lab <- ifelse(lm_mean_tidy$p.value < 0.05, 'p < 0.05', 'p > 0.05') lm_max_tidy$lab <- ifelse(lm_max_tidy$p.value < 0.05, 'p < 0.05', 'p > 0.05')

df_lab <- data.frame(min = lm_min_tidy$lab, mean = lm_mean_tidy$lab, max = lm_max_tidy$lab, stringsAsFactors = F)

return(df_lab) }`

padilla410 commented 6 years ago

update. This works: `my_lm_p_labs <- function(dat_in) {

generate data summary

summary <- dat_in %>% group_by(season) %>% summarise(count = sum(is.na(min)))

return(summary)

remove seasons with out results

dat_in <- dat_in[complete.cases(dat_in), ]

lm_results <- dat_in %>% group_by(.data$season) %>% do(reg_min = lm(.data$min ~ .data$year, data = .data, na.action = 'na.omit') , reg_mean = lm(.data$mean ~ .data$year, data = .data, na.action = 'na.omit') , reg_max = lm(.data$max ~ .data$year), data = .data, na.action = 'na.omit')

lm_min_tidy <- tidy(lm_results, reg_min) %>% filter(.data$term == '.data$year') lm_mean_tidy <- tidy(lm_results, reg_mean) %>% filter(.data$term == '.data$year') lm_max_tidy <- tidy(lm_results, reg_max) %>% filter(.data$term == '.data$year')

lm_min_tidy$lab <- ifelse(lm_min_tidy$p.value < 0.05, 'p < 0.05', 'p > 0.05') lm_mean_tidy$lab <- ifelse(lm_mean_tidy$p.value < 0.05, 'p < 0.05', 'p > 0.05') lm_max_tidy$lab <- ifelse(lm_max_tidy$p.value < 0.05, 'p < 0.05', 'p > 0.05')

df_lab <- data.frame(season = lm_min_tidy$season, min = lm_min_tidy$lab, mean = lm_mean_tidy$lab, max = lm_max_tidy$lab, stringsAsFactors = F)

reinsert missing levels

df_lab <- tidyr::complete(df_lab, season)#!! seas)

replace NA values with blank text

df_lab[is.na(df_lab)] <- ''

return(df_lab) }`