I have a dataset in long form, values in "values" column and categories in "ind". The data looks like this:
values ind
1 42.58666667 le_mean
2 52.35666667 le_mean
64 78.7 le_mean
65 95.49666667 le_mean
66 88.91 le_mean
67 1.295234856 le_sd
68 4.294139417 le_sd
69 0 le_sd
70 7.327416552 le_sd
71 4.007322464 le_sd
72 0 le_sd
73 0 le_sd
74 0 le_sd
75 0 le_sd
76 0.704367328 le_sd
77 1.11 le_sd
78 6.870315374 le_sd
79 10.36559855 le_sd
80 7.589591557 le_sd
86 1.223165293 le_sd
87 7.600019737 le_sd
88 3.655995077 le_sd
89 5.148595278 le_sd
90 0 le_sd
229 2.385211381 re_sd
230 4.465672775 re_sd
231 3.129765699 re_sd
232 3.55056803 re_sd
233 0 re_sd
234 0 re_sd
276 29.34 lf_mean
277 41.66333333 lf_mean
278 39.84666667 lf_mean
279 35.33666667 lf_mean
280 61.68 lf_mean
281 73.22333333 lf_mean
282 75.51666667 lf_mean
283 31.74666667 lf_mean
284 28.37666667 lf_mean
285 40.03333333 lf_mean
286 21.31333333 lf_mean
287 18.90666667 lf_mean
288 0 lf_mean
I am trying to get the p-values for a shapiro.test out in a data frame by category, but I am getting the same p-values, which is incorrect. I have tried:
ddply(bpdata_long, .(ind),
function(x) shapiro.test(bpdata_long$values)$p.value)
and I have also tried:
shapfunc <- function(x){
return(data.frame(pvalues=shapiro.test(bpdata_long$values)$p.value))
}
ddply(bpdata_long, .(ind), shapfunc)
but with both all I'm getting back is:
ddply(bpdata_long, .(ind),
function(x) shapiro.test(bpdata_long$values)$p.value)
ind V1
1 le_mean 0.0000000000000000000000000000008028749
2 le_sd 0.0000000000000000000000000000008028749
3 re_mean 0.0000000000000000000000000000008028749
4 re_sd 0.0000000000000000000000000000008028749
5 lf_mean 0.0000000000000000000000000000008028749
6 lf_sd 0.0000000000000000000000000000008028749
Could someone help with this, please? Where does my code go wrong?
CodePudding user response:
The issue is that by using shapiro.test(bpdata_long$values)
you apply the Shapiro test on the ungrouped values
column. That's why you get the same value for each group. Additionally, instead of using the retired plyr
package I would suggest to switch to dplyr
:
library(dplyr)
bpdata_long %>%
group_by(ind) %>%
summarise(p.value = shapiro.test(values)$p.value)
#> # A tibble: 4 × 2
#> ind p.value
#> <chr> <dbl>
#> 1 le_mean 0.450
#> 2 le_sd 0.00774
#> 3 lf_mean 0.471
#> 4 re_sd 0.285