0% found this document useful (0 votes)
54 views2 pages

Exam Cheatsheet For R Langauge Coding

This document provides a summary of various statistical and data analysis functions in R grouped by their general purpose: 1) The first section lists various R libraries and their functions for data import, visualization, summary statistics, and regression analysis. 2) Next, common data management tasks are covered like subsetting data, creating new variables, checking variable classes, and removing variables. 3) Visualization techniques like histograms, density plots, scatterplots, and pie charts are then described along with the ggplot and gtsummary packages. 4) Regression analysis and robust standard errors are explained last along with functions for outputting tables and exporting results.

Uploaded by

amir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views2 pages

Exam Cheatsheet For R Langauge Coding

This document provides a summary of various statistical and data analysis functions in R grouped by their general purpose: 1) The first section lists various R libraries and their functions for data import, visualization, summary statistics, and regression analysis. 2) Next, common data management tasks are covered like subsetting data, creating new variables, checking variable classes, and removing variables. 3) Visualization techniques like histograms, density plots, scatterplots, and pie charts are then described along with the ggplot and gtsummary packages. 4) Regression analysis and robust standard errors are explained last along with functions for outputting tables and exporting results.

Uploaded by

amir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Group Function

Libraries library(“tidyverse”) library(“ggplot2”)


library(“psych”) library(“gtsummary”)
library(“stargazer”) library(“fastDummies”)
library(“dplyr”) library("lmtest")
library(“readr”) library("estimatr")
library(“haven”) library("broom")
library("texreg")

Opening a file library(readr) library(haven)


D<-read_csv("file_address/filename.csv") D<- read_dta("file_address/filename.dta")

Viewing View(D[V,V]) head(D[,c("V", "V")])


View(D[,c("V", "V")]) tail(D[,c("V", "V")])

Information summary(D$V)

table(D$V)

library(‘psych’)
descriptive<-describe(D)
descriptive[c("V", "V", "V"), c("n", "mean", "sd", "min", "max")]

Subset:
New_D<-subset(D,lookup in V==criteria,select=c(V,V,V))
(It creates a new data not a new variable)

General c(“V”,”V”)]) D$V<-log(D$V+1)


name=c("V", "V") dim(D): number of obs and variables
D$new V<- D$old V str(D$V): Shows sample of obs
D$V<-NULL class(D$V): Checks if the var is numeric or factor
attr(D$V, which="label")<- “name 1, name 2”

If function D$V=ifelse(D$V>=,true condition,false condition)

Histogram hist(D$V) : Simple histogram

hist(D$V, nclass = 1) : Creates histogram (nclass : Determines the number of bars)

hist(log(D$V+1), nclass = 100)

Plot/ Density plot plot(density(D$V, na.rm=T)) : Removes empty cells and provides a density plot.

plot(density(log(D$V+1), na.rm=T )) : Same as previous but logs it too

Scatterplot plot(log(D$V+1), D$V)

ggplot ggplot(data = D, mapping = aes(x = V, y = V)) Arguments:


alpha=0.1
labs(title = "new_title", x = “new_name", y = "new_name") color= “blue”
fill=”black”
theme_minimal() / theme_classic() : add or remove gridlines size=2
method=lm (for trendline)
Geoms: se=f (for trendline)
geom_point(): For scatter plots, dot plots, etc. binwidth= 5000 (for histogram)
geom_boxplot(): For, well, boxplots! show.legend=F (for boxplot)
geom_line(): For trend lines, time series, etc.
geom_histogram(): for histograms
geom_smooth(): for trendline on scatterplots
geom_density(): for density plot
geom_bar: creating a bar chart

Stargazer library library('stargazer')


stargazer(D[,c('V', 'V')],type= "text")
stargazer(data.frame(rlms2016[,c('income', 'bmi')],type= "text")

dplyr library library(‘dplyr’)


Mutate(D,New_V=Some_Function)
D<-Mutate(D,New_V=Some_Function) (To assign the new variable to the dataset)

Pie Chart pie(table(D$V))

Gtsummary library(gtsummary)
library is.numeric(D$V)
D$V<-as.factor(D$V)
tbl_summary(D[,c("V","V","V")],
missing = "no",
statistic = list(all_continuous()~"{N_nonmiss}/{mean}/{sd}/{min}/{max}"))

rlms2016$educ<-factor(rlms2016$educ,
levels = c(0,1,2,3,4),
labels= c("L1", "L2","L3","L4","L5"))

library(dplyr)
rlms2016$male<-rlms2016$H5
rlms2016$male<-ifelse(rlms2016$H5==2,0,NA)
rlms2016$male<-ifelse(rlms2016$H5==1,1,rlms2016$male)
table(rlms2016$male)

rlms2016 %>%
select(income,bmi,educ,male) %>%
tbl_summary(by=male,
missing = "no",
statistic = list(all_continuous()~"{N_nonmiss}/{mean}/{sd}/{min}/{max}")) %>%

add_p(all_continuous()~"t.test") : adds a p value column to the table and a t test

Dummy columns library("dplyr") library("stargazer")


library("fastDummies") stargazer(Sample_Exam,type="text")

industries = dummy_cols(Sample_Exam$Indsutry, split = T, stargazer(data.frame(Sample_Exam %>%


remove_selected_columns = T) %>% select(-Industry))
rename('Industry_1' = '.data_1', 'Industry_2' = '.data_2', ,type="text")
'Industry_3' = '.data_3')

Sample_Exam = cbind(Sample_Exam, industries)

Regression lm("ROI~Size",Sample_Exam)

model1<-lm("ROI~Size",Sample_Exam)
summary(model1)
model2<-lm("ROI~Size",Sample_Exam)
summary(model2)
model2<-lm("ROI~Size+RDtoSales",Sample_Exam)
summary(model2)

Sample_Exam$Industry<-as.factor(Sample_Exam$Industry)
model2<-lm("ROI~log(size+1)+RDtoSales+Indsutry",Sample_Exam)

stargazer(model1,model2,type="text")

Robust s.e library("lmtest") library("estimatr") library("broom") library("texreg")

model2r<- lm_robust(ROI~log(Size+1)+RDtoSales+Industry,Sample_Exam,se_type+"stata")

summary(model2r)

screenreg(list(model1,model2,model2r))

# to export the tables created above


htmlreg(list(model1,model2,model2r))

Exporting Exporting a table:


Stargazer package
stargazer( model(for example), type=“html”, out=“name.html”)
Then on the right you should check for new file and click on More then export

If you want to export summary statistics table:


stargazer(as.data.frame(subset name), type=“html”, out=“name.html”)

You might also like