Output the dataframe with coefficient and significance

Is it possible to output dataframe with correlation coefficients result and mark with symbol *if significance, rather than drawing graphs?mtcars data in R or other data else as example is ok.
Any help is welcome.

Will this float your boat?

library("tidyverse")
library("broom")
mtcars %>% 
  group_by(gear, am) %>% 
  nest %>% 
  mutate(mdl = map(data, ~lm(mpg ~ wt, data = .x)),
         mdl_tidy = map(mdl, tidy),
         mdl_summary = map(mdl, glance)) %>% 
  unnest(c(mdl_tidy, mdl_summary), names_repair = "unique") %>% 
  select(am, gear, term, estimate, r.squared, p.value = p.value...9) %>% 
  mutate(significance = case_when(p.value < 0.001 ~ "***",
                                  p.value < 0.01 ~ "**",
                                  p.value < 0.05 ~ "*",
                                  TRUE ~ "ns."))

Yielding

# A tibble: 8 × 7
     am  gear term        estimate r.squared     p.value significance
  <dbl> <dbl> <chr>          <dbl>     <dbl>       <dbl> <chr>       
1     1     4 (Intercept)    48.4      0.688 0.000231    ***         
2     1     4 wt             -9.75     0.688 0.0109      *           
3     0     3 (Intercept)    28.4      0.608 0.000000148 ***         
4     0     3 wt             -3.16     0.608 0.000605    ***         
5     0     4 (Intercept)    81.4      0.868 0.0393      *           
6     0     4 wt            -18.2      0.868 0.0682      ns.         
7     1     5 (Intercept)    42.6      0.979 0.000180    ***         
8     1     5 wt             -8.05     0.979 0.00128     **          

Thank you for your effort and help on this question, it helped me.
Actually, I just want to get the correlation matrix(compute correlation coefficient between different variable ),and mark if the correlation is significant in R.Like the following picture.

Ok, that could be coded up, but perhaps this will be sufficient:

corrplot::corrplot(cor(mtcars), method = 'number')
1 Like

Is it possible to ouput a dataframe of correlation coeffecient , and if it is significant mark it with"*", like the picture in my former post? Maybe not a plot.
Thank you very much.

Try this

round(cor(mtcars), 3)

Yielding:

        mpg    cyl   disp     hp   drat     wt   qsec     vs     am   gear   carb
mpg   1.000 -0.852 -0.848 -0.776  0.681 -0.868  0.419  0.664  0.600  0.480 -0.551
cyl  -0.852  1.000  0.902  0.832 -0.700  0.782 -0.591 -0.811 -0.523 -0.493  0.527
disp -0.848  0.902  1.000  0.791 -0.710  0.888 -0.434 -0.710 -0.591 -0.556  0.395
hp   -0.776  0.832  0.791  1.000 -0.449  0.659 -0.708 -0.723 -0.243 -0.126  0.750
drat  0.681 -0.700 -0.710 -0.449  1.000 -0.712  0.091  0.440  0.713  0.700 -0.091
wt   -0.868  0.782  0.888  0.659 -0.712  1.000 -0.175 -0.555 -0.692 -0.583  0.428
qsec  0.419 -0.591 -0.434 -0.708  0.091 -0.175  1.000  0.745 -0.230 -0.213 -0.656
vs    0.664 -0.811 -0.710 -0.723  0.440 -0.555  0.745  1.000  0.168  0.206 -0.570
am    0.600 -0.523 -0.591 -0.243  0.713 -0.692 -0.230  0.168  1.000  0.794  0.058
gear  0.480 -0.493 -0.556 -0.126  0.700 -0.583 -0.213  0.206  0.794  1.000  0.274
carb -0.551  0.527  0.395  0.750 -0.091  0.428 -0.656 -0.570  0.058  0.274  1.000

@Leon Good job. It has almost solved my problem.
Is it possible to mark * on a significant relevant number according to the p value in this dataframe?

1 Like

Yes, but there is a fine line between helping someone and solving their problem for them, so what did you try?

Thank you for your help, I will try again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.