'Summary of descriptive data
i got a panel data set. I am analysing name changes of mutual funds. I created 6m prior & 6m past name-change Mean & Median values for various variables.
I want to summarise the results like this just without the t-test:
I tried stargazer. But if i insert more than 2 DFs i get an error: Error in if (is.na(s)) { : the condition has length > 1. And it doesn't look anything close to the above anyways.
My data is stored / created as follows:
library(readxl)
New_R_done <- read_excel("New_R done.xlsx")
View(New_R_done)
library(tidyverse)
library(plm)
descr <- pdata.frame(New_R_done, index=c("ISIN", "Date"))
str(descr)
length(levels(as.factor(descr$Dummy)))
length(levels(as.factor(descr$ISIN)))
descr <- descr %>% na.omit()
View(descr)
#Return
descr$Returns <- ifelse(descr$Returns=="n.a", 0, descr$Returns)
descr$Returns <- as.numeric(descr$Returns)
#Flows
descr$Flows <- ifelse(descr$Flows=="n.a", 0, descr$Flows)
descr$Flows <- as.numeric(descr$Flows)
#TNA
descr$TNA <- ifelse(descr$TNA=="n.a", 0, descr$TNA)
descr$TNA <- as.numeric(descr$TNA)
name_change <- ifelse(descr$Dummy==1 & lag(descr$Dummy, 1)==0, 1, 0)
# Median Return
lags <- plm::lag(descr$Returns, 1:6)
lag_median <- apply(lags, 1, median)
leads <- plm::lead(descr$Returns, 1:6)
lead_median <- apply(leads, 1, median)
d <- data.frame(name_change, lag_median, lead_median)
d[d$name_change==1,]
# Median Flows
lags_flow <- plm::lag(descr$Flows, 1:6)
lag_median_flow <- apply(lags_flow, 1, median)
leads_flow <- plm::lead(descr$Flows, 1:6)
lead_median_flow <- apply(leads_flow, 1, median)
d_flow <- data.frame(name_change, lag_median_flow, lead_median_flow)
d_flow[d_flow$name_change==1,]
#Median TNA
lags_TNA <- plm::lag(descr$TNA, 1:6)
lag_median_TNA <- apply(lags_TNA, 1, median)
leads_TNA <- plm::lead(descr$TNA, 1:6)
lead_median_TNA <- apply(leads_TNA, 1, median)
d_TNA <- data.frame(name_change, lag_median_TNA, lead_median_TNA)
d_TNA[d_TNA$name_change==1,]
# Mean Return
lag_mean <- apply(lags, 1, mean)
lead_mean <- apply(leads, 1, mean)
d_m <- data.frame(name_change, lag_mean, lead_mean)
d_m[d_m$name_change==1,]
# Mean Flows
lag_mean_flow <- apply(lags_flow, 1, mean)
lead_mean_flow <- apply(leads_flow, 1, mean)
d_flow_mean <- data.frame(name_change, lag_mean_flow, lead_mean_flow)
d_flow_mean[d_flow_mean$name_change==1,]
# Mean TNA
lag_mean_tna <- apply(lags_TNA, 1, mean)
lead_mean_tna <- apply(leads_TNA, 1, mean)
d_tna_mean <- data.frame(name_change, lag_mean_tna, lead_mean_tna)
d_tna_mean[d_tna_mean$name_change==1,]
# Return Median +6 -6
makeRETURN <- d[d$name_change==1,]
mean(makeRETURN$lag_median)
mean(makeRETURN$lead_median, na.rm = TRUE)
# Flow Median +6 -6
makeFlOW <- d_flow[d_flow$name_change==1,]
mean(makeFlOW$lag_median_flow)
mean(makeFlOW$lead_median_flow, na.rm = TRUE)
# TNA Median +6 -6
makeTNA <- d_TNA[d_TNA$name_change==1,]
mean(makeTNA$lag_median_TNA)
mean(makeTNA$lead_median_TNA, na.rm = TRUE)
# Return Mean +6 -6
makeRETURN_mean <- d_m[d_m$name_change==1,]
mean(makeRETURN_mean$lag_mean)
mean(makeRETURN_mean$lead_mean, na.rm = TRUE)
# Flow Mean +6 -6
makeFLOW_mean <- d_flow_mean[d_flow$name_change==1,]
mean(makeFLOW_mean$lag_mean_flow)
mean(makeFLOW_mean$lead_mean_flow, na.rm = TRUE)
# TNA Mean +6 -6
makeTNA_mean <- d_tna_mean[d_TNA$name_change==1,]
mean(makeTNA_mean$lag_mean_tna)
mean(makeTNA_mean$lead_mean_tna, na.rm = TRUE)
library(stargazer)
#how to display a matrix with Return -- Mean - Median for +6 -6 ?
stargazer(makeTNA_mean, makeFLOW_mean, makeRETURN_mean, makeTNA, makeFlOW, makeRETURN, type = "text")
The stargazer part at the end doesn't work.
highly apreciate any help!
Cheers, Joe
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|