knitr::opts_chunk$set(echo = TRUE, warning=FALSE,comment = NA, message=FALSE,
fig.height=4, fig.width=6)
Use the data on price pass through in meat to estimate a regression of price change at retail on price change at the farm gate. Report a regression table from R. Interpret the coefficient on price change at the farm gate. Interpret the t-statistics and the p-value reported by R. Is this effect of price change at the farm gate statistically significant at the 5% level?
library(stargazer)
library(tidyr)
library(ggplot2)
library(plyr)
retail <- read.csv("C:\\Users\\user\\Downloads\\retail.csv")
head(retail,5)
attach(retail)
my_model <- lm(retail$change_retail_value~retail$change_farm_value, data=retail)
stargazer(my_model, report = "vc*stp",type = "text",out = "./q7results.txt")
===============================================
Dependent variable:
---------------------------
change_retail_value
-----------------------------------------------
change_farm_value 0.314***
(0.044)
t = 7.120
p = 0.000
Constant 0.952***
(0.336)
t = 2.834
p = 0.005
-----------------------------------------------
Observations 623
R2 0.075
Adjusted R2 0.074
Residual Std. Error 8.369 (df = 621)
F Statistic 50.689*** (df = 1; 621)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01
library(texreg)
screenreg(my_model, digits = 2, title = "Regression Results", caption = "Source: retail dataset")
====================================
Model 1
------------------------------------
(Intercept) 0.95 **
(0.34)
retail$change_farm_value 0.31 ***
(0.04)
------------------------------------
R^2 0.08
Adj. R^2 0.07
Num. obs. 623
====================================
*** p < 0.001; ** p < 0.01; * p < 0.05
library(rempsyc)
library(flextable)
library(report)
library(broom)
library(gapminder)
library(xtable)
data("mtcars")
head(mtcars,6)
nice_t_test(data = mtcars,
response = "mpg",
group = "vs",
warning = FALSE)
nice_t_test(
data = mtcars,
response = names(mtcars)[1:6],
group = "am",
warning = FALSE)
t.test.results <- nice_t_test(
data = mtcars,
response = names(mtcars)[1:6],
group = "am",
warning = FALSE)
t.test.results
my_table <- nice_table(t.test.results)
my_table
Dependent Variable | t | df | p | d | 95% CI |
|---|---|---|---|---|---|
mpg | -3.77 | 18.33 | .001 | -1.48 | [-2.27, -0.67] |
cyl | 3.35 | 25.85 | .002 | 1.21 | [0.43, 1.97] |
disp | 4.20 | 29.26 | < .001 | 1.45 | [0.64, 2.23] |
hp | 1.27 | 18.72 | .221 | 0.49 | [-0.23, 1.21] |
drat | -5.65 | 27.20 | < .001 | -2.00 | [-2.86, -1.12] |
wt | 5.49 | 29.23 | < .001 | 1.89 | [1.03, 2.73] |
nice_t_test(data = mtcars,
response = "mpg",
group = "am",
alternative = "greater",
warning = FALSE) |>
nice_table()
Dependent Variable | t | df | p | d | 95% CI |
|---|---|---|---|---|---|
mpg | -3.77 | 18.33 | .999 | -1.48 | [-2.27, -0.67] |
nice_t_test(data = mtcars,
response = "mpg",
mu = 17,
alternative = "less",
warning = FALSE) |>
nice_table()
Dependent Variable | t | df | p | d | 95% CI |
|---|---|---|---|---|---|
mpg | 2.90 | 31 | .997 | 0.51 | [0.14, 0.88] |
data("ToothGrowth")
head(ToothGrowth,5)
nice_t_test(data = ToothGrowth,
response = "len",
group = "supp",
paired = TRUE) |>
nice_table()
Dependent Variable | t | df | p | d | 95% CI |
|---|---|---|---|---|---|
len | 3.30 | 29 | .003 | 0.60 | [0.21, 0.99] |
There are other ways to do t-tests and format the results properly, should you wish—for example through the broom and report packages. Examples below.
model <- t.test(mpg ~ am, alternative="two.sided", data = mtcars)
stats.table <- tidy(model, conf.int = TRUE)
nice_table(stats.table, broom = "t.test")
Method | Alternative | Mean 1 | Mean 2 | M1 - M2 | t | df | p | 95% CI |
|---|---|---|---|---|---|---|---|---|
Welch Two Sample t-test | two.sided | 17.15 | 24.39 | -7.24 | -3.77 | 18.33 | .001 | [-11.28, -3.21] |
(stats.table <- as.data.frame(report(model)))
nice_table(stats.table, report = "t.test")
Method | Alternative | Parameter | Group | Mean_Group1 | Mean_Group2 | Difference | t | 95% CI (t) | df | p | d | 95% CI (d) |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Welch Two Sample t-test | two.sided | mpg | am | 17.15 | 24.39 | -7.24 | -3.77 | [-11.28, -3.21] | 18.33 | .001 | -1.76 | [-2.82, -0.67] |
library(readxl)
library(dplyr)
library(tibble)
library(flextable)
data_anova <- read.csv("C:\\Users\\user\\Downloads\\data_anova.csv")
head(data_anova,5)
data_anova$Rep = as.factor(data_anova$Rep)
data_anova$Water = as.factor(data_anova$Water)
data_anova$Priming = as.factor(data_anova$Priming)
attach(data_anova)
for(i in 1:ncol(data_anova[-c(1:3)])) {
cols <- names(data_anova)[4:ncol(data_anova)]
aov.model <- lapply(X = cols, FUN = function(x)
aov(reformulate(termlabels = "Rep + Water*Priming",
response = x),
data = data_anova))
# print df, MS and Pvalue
final = anova(aov.model[[i]])[,c(1,3,5)]
# Getting rownames
rnames = rownames(final)
# Setting column names
colnames(final) = c("DF", "MS", "P-value")
colnames(final)[2] = cols[i]
# Rounding values to 3 decimal place
final = as.data.frame(round(final, digits = 2))
# Assign astericks according to p values
final$sign[final$`P-value` < 0.1] <- "*"
final$sign[final$`P-value` < 0.05] <- "**"
final$sign[final$`P-value` < 0.01] <- "***"
final$sign[final$`P-value` > 0.01] <- "ns"
# Merge MS and significance column together
final[[2]] = paste(final[[2]],
ifelse(is.na(final[[4]]), "", final[[4]]))
final = final[-c(3,4)]
anova = writexl::write_xlsx(final,
path = paste(cols[i], '-ANOVA.xlsx'))
# Print final ANOVA table ----
file.list <- list.files(pattern='*-ANOVA.xlsx')
df.list <- lapply(X = file.list, FUN = read_excel)
# Combined ANOVA table for all variables
aov.table = rlist::list.cbind(df.list)
# Remove duplicate columns for DF
dup.cols = which(duplicated(names(aov.table)))
aov.table = aov.table[,-dup.cols]
# Names for sources of variation in ANOVA
rownames(aov.table) = rnames
}
table = flextable(data = aov.table %>%
rownames_to_column("SOV"))
bold(table, bold = TRUE, part = "header")
SOV | DF | Biological.yield | Grain.per.spike | grain.weight | Grain.yield | Plant.height | Spike.length | Spikelets |
|---|---|---|---|---|---|---|---|---|
Rep | 2 | 1.93 ns | 24.11 ns | 79.25 *** | 0.27 *** | 0.44 ns | 8.72 ** | 7966.37 *** |
Water | 2 | 0.2 ns | 21.44 ns | 3.4 ns | 0.2 ** | 30.33 ns | 1.04 ns | 270.7 ns |
Priming | 2 | 46.74 *** | 1374.33 *** | 836.53 *** | 8.58 *** | 0.33 ns | 40.47 *** | 14152.15 *** |
Water:Priming | 4 | 0.18 ns | 30.44 ns | 7.03 ns | 0.02 ns | 16.83 ns | 1.42 ns | 230.59 ns |
Residuals | 16 | 0.7 | 41.82 | 5.22 | 0.03 | 15.32 | 1.25 | 710.29 |