I'm trying to create descriptive statistics in a "publishable" html format.
Let's take the mtcars data and assume I want to create a table that gives me the usual descriptive statistics for Miles/(US) gallon, Gross horsepower, Weight (1000 lbs), and 1/4 mile time for both automatic and manual cars.
I can get a rough version of what I am looking for by using psych::describeBy
library(tidyverse)
library(psych)
#Descriptive statistics
data("mtcars")
df <- mtcars|>
select(1,4,6,7,9)
describeBy(df, group = mtcars$am, fast=TRUE)
However, I am trying to create this in a format that is close to what you would find in journal articles and also can be exported as html. Anyone got any suggestions? I tried to use stargazer but struggled to get results for both groups in one table.
Thanks!
Here is minimal raw data for mtcars grouped by am with gtsummary:
library(tidyverse)
data(mtcars)
as_tibble(mtcars)
library(gtsummary)
mtcars %>% tbl_summary(by = am)
You can have various modifications (labels etc.) in gtsummary.
I have to compute characteristic roots for 100s of U.S.counties. I got help using lapply. I prefer using MAP and PURR if that is possible. I am using them to compute linear regressions.
Here is my test problem and the output.
test.dat<- tibble(ID=c(1,2),a=c(1,1),b=c(1,1),c=c(2,2),d=c(4,3))
test.out<-test.dat %>% nest(-ID) %>% mutate(fit = purrr::map(data,~ function(x) eigen(data.matrix(x)), data=.))
I would appreciate any help.
Thanks
V.K.Chetty
I would like to ask for tips on how to execute multiple statistical test(e.g. t-test, f-test, ks-test) for multiple groups. Basically, I want to run the statistical tests as many times as the number of groups available in my data and come up with a single result. In this case, I am trying to do a t-test to compare current and previous year data grouped by a variable(let's say dealer code).
I have a data similar to the below (stress) and would like to find out if there is a generic approach on how to do this? I managed to use rstatix for t-test but it doesn't have this function for f-test and ks-test.
Any help is appreciated. Thank you!
library(datarium)
library(rstatix)
data("stress", package = "datarium")
set.seed(123)
stress%>% sample_n_by( size = 60)
stat.test <- stress %>%
group_by(exercise) %>%
t_test(score~ treatment) %>%
add_significance()
stat.test
I'm trying to make a loop (or something else that can do this) that can run a linear model of the year and natural log of cases from my data, for each country, separately so that I can gain a slope from each linear model and plot them as a histogram.
I'm very new to R and I'm struggling immensely to work out how to do this; below is a rough snapshot of what my data looks like, and has 197 different countries in total, ranging from years 1997 - 2019.
data
Any help on how to do this would be greatly appreciated, thank you.
Based on your question, take a look at this website.
Let's say your data is in a data.frame called df, then you could
df %>%
split(.$country) %>%
map(~ lm(log(cases) ~ year, data = .)) %>%
map(summary) %>%
map_dbl("Estimate")
Let me know if you need more help with this.
This is complete reEdit of my orignal question
Let's assume I'm working on RT data gathered in a repeated measure experiment. As part of my usual routine I always transform RT to natural logarytms and then compute a Z score for each RT within each partipant adjusting for trial number. This is typically done with a simple regression in SPSS syntax:
split file by subject.
REGRESSION
/MISSING LISTWISE
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT rtLN
/METHOD=ENTER trial
/SAVE ZRESID.
split file off.
To reproduce same procedure in R generate data:
#load libraries
library(dplyr); library(magrittr)
#generate data
ob<-c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3)
ob<-factor(ob)
trial<-c(1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6)
rt<-c(300,305,290,315,320,320,350,355,330,365,370,370,560,565,570,575,560,570)
cond<-c("first","first","first","snd","snd","snd","first","first","first","snd","snd","snd","first","first","first","snd","snd","snd")
#Following variable is what I would get after using SPSS code
ZreSPSS<-c(0.4207,0.44871,-1.7779,0.47787,0.47958,-0.04897,0.45954,0.45487,-1.7962,0.43034,0.41075,0.0407,-0.6037,0.0113,0.61928,1.22038,-1.32533,0.07806)
sym<-data.frame(ob, trial, rt, cond, ZreSPSS)
I could apply a formula (blend of Mark's and Daniel's solution) to compute residuals from a lm(log(rt)~trial) regression but for some reason group_by is not working here
sym %<>%
group_by (ob) %>%
mutate(z=residuals(lm(log(rt)~trial)),
obM=mean(rt), obSd=sd(rt), zRev=z*obSd+obM)
Resulting values clearly show that grouping hasn't kicked in.
Any idea why it didn't work out?
Using dplyr and magrittr, you should be able to calculate z-scores within individual with this code (it breaks things into the groups you tell it to, then calculates within that group).
experiment %<>%
group_by(subject) %>%
mutate(rtLN = log(rt)
, ZRE1 = scale(rtLN))
You should then be able to do use that in your model. However, one thing that may help your shift to R thinking is that you can likely build your model directly, instead of having to make all of these columns ahead of time. For example, using lme4 to treat subject as a random variable:
withRandVar <-
lmer(log(rt) ~ cond + (1|as.factor(subject))
, data = experiment)
Then, the residuals should already be on the correct scale. Further, if you use the z-scores, you probably should be plotting on that scale. I am not actually sure what running with the z-scores as the response gains you -- it seems like you would lose information about the degree of difference between the groups.
That is, if the groups are tight, but the difference between them varies by subject, a z-score may always show them as a similar number of z-scores away. Imagine, for example, that you have two subjects, one scores (1,1,1) on condition A and (3,3,3) on condition B, and a second subject that scores (1,1,1) and (5,5,5) -- both will give z-scores of (-.9,-.9,-.9) vs (.9,.9,.9) -- losing the information that the difference between A and B is larger in subject 2.
If, however, you really want to convert back, you can probably use this to store the subject means and sds, then multiply the residuals by subjSD and add subjMean.
experiment %<>%
group_by(subject) %>%
mutate(rtLN = log(rt)
, ZRE1 = scale(rtLN)
, subjMean = mean(rtLN)
, subjSD = sd(rtLN))
mylm <- lm(x~y)
rstandard(mylm)
This returns the standardized residuals of the function. To bind these to a variable you can do:
zresid <- rstandard(mylm)
EXAMPLE:
a<-rnorm(1:10,10)
b<-rnorm(1:10,10)
mylm <- lm(a~b)
mylm.zresid<-rstandard(mylm)
See also:
summary(mylm)
and
mylm$coefficients
mylm$fitted.values
mylm$xlevels
mylm$residuals
mylm$assign
mylm$call
mylm$effects
mylm$qr
mylm$terms
mylm$rank
mylm$df.residual
mylm$model