Quantcast
Channel: Active questions tagged lm - Stack Overflow
Viewing all articles
Browse latest Browse all 124

Convert two-tailed p-values to one-tailed p-values in lm and glm output (and retain model class) [closed]

$
0
0

THIS IS A PROGRAMMING QUESTION; ADMINS CLEARLY HAVEN'T READ THE QUESTION BEFORE CLOSING IT. I'M STILL KEEN TO HEAR PEOPLE'S THOUGHTS ON POSSIBLE SOLUTIONS. THANK YOU!

Say I have the following data:

library(ISLR2) #imports Auto dflibrary(tidyverse)auto <- Auto %>%   mutate(mpg_mean = case_when(mpg <= mean(mpg) ~ 0, TRUE ~ 1 ))

I've written a function that converts the p-values of an lm object or glm object into one-sided p-values. However, I want the output of the function to be an lm or glm object (depending on what I feed it) as I subsequently want to feed my models into stargazer(). As stargazer does not accept summary.lm or summary.glm objects, i'm struggling to see how I can do this.

For example, this is the function i've got:

convert_p_values_one_tailed <- function(output) {  convert_p_value <- function(p) {    return(p / 2)  }  if (inherits(output, "lm") & !inherits(output, "glm")) {    # If the input is an lm object, modify the summary directly    #summary_output <- summary(output)    #summary_output$coefficients[, "Pr(>|t|)"] <- convert_p_value(summary_output$coefficients[, "Pr(>|t|)"])    #return(summary_output)    output$coefficients[, "Pr(>|t|)"] <- convert_p_value(output$coefficients[, "Pr(>|t|)"])    return(output)  } else if (inherits(output, "glm")) {    # If the output is a glm object    output$coefficients[, "Pr(>|z|)"] <- convert_p_value(output$coefficients[, "Pr(>|z|)"])    return(output)  } else if ("p.value" %in% colnames(output)) {    # If the input is a tidy data frame, modify the p-values    output <- output %>%      mutate(p.value = convert_p_value(p.value))    return(output)  } else {    stop("The input must be either an lm object, a glm object, or a tidy data frame with a 'p.value' column.")  }}

Running these models and calling the function:

#lm modellm_mod <- lm(as.numeric(mpg_mean) ~ as.factor(cylinders),          data = auto) %>%  convert_p_values_one_tailed()#glm modelglm_model <- glm(as.numeric(mpg_mean) ~ as.factor(cylinders),                  family = binomial(link = "logit"),                data = auto) %>%  convert_p_values_one_tailed()

Returns the following error:

Error in output$coefficients[, "Pr(>|t|)"] :   incorrect number of dimensions#or |z| for the logit model

I have got my function to do what I want by converting to summary.lm or summary.glm first, as per the commented out lines in the if lm = TRUE chunk, but this makes it impossible to feed my models into stargazer which is the next step in my analytical flow.

Does anyone know how I can square this circle? Is it possible to access (and manipulate) the p-values of an lm/glm class object directly? If so, how?


Viewing all articles
Browse latest Browse all 124

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>