Purtest Object - How to save Output as tex file? - r

I was wondering whether there is a way to save a purtest - output as a LaTeX
file?
As you can see in the example-code, I have already tried to produce it via stargazer. However, the stargazer function does not support the purtest-class.
library(plm)
library(stargazer)
dat <- data.frame(entity = c(rep("a",10),rep("b",10)),year =
rep(1970:1979,2),value = rnorm(20))
pdat <- pdata.frame(dat,index = c("entity","year"))
res <- purtest(object = pdat$value,test = "ips",exo = "intercept",pmax = 1)
stargazer(summary(res),type = "latex")
I know that it is possible to extract values manually, to store them in a data.frame and eventually to save the data.frame via print.xtable as a LaTeX file.
But perhaps there is any neat solution to the problem.

Stargazer library has a lot of checks constraining the classes that can be used.
Class "purtest" is not included, but as Stargazer support exporting of "matrix" class, one can trick the restrictions. For example:
# the problem
library(plm)
library(stargazer)
dat <- data.frame(entity = c(rep("a",10),rep("b",10)),year =
rep(1970:1979,2),value = rnorm(20))
pdat <- pdata.frame(dat,index = c("entity","year"))
res <- purtest(object = pdat$value,test = "ips",exo = "intercept",pmax = 1)
# One solution: extract the parametars and place them in the matrix:
a = unlist(res$idres[[1]])
b = unlist(res$idres[[2]])
all = rbind(a, b)
class(all) <- c("matrix")
stargazer(all,type = "latex",align = T)
# need to align , else you get strange double dollar signs

Related

Creating a loop in R to name (save) files based on the name of the variable they're stored in

this is probably a basic question but I still need some help to figure out what I should do.
My code is very simple, there are a bunch of calculations to get raster files stored in variables :NDWI,NDWI, VSI, etc. each calculation is based on an satellite image which has a name with the date in it. I will find a way to extract the date and store it in a variable.
In essence, what I want is a part of my code that goes over all the files created to save them as raster in a specific path of my laptop, under this format ( "variable name"_"date".tif. )
What I have for now :
Dossier <- "C:/Users/Perrin/Desktop/INRA/Raster/sentinel/L1C_T31UDR_A019210_20190225T105315/S2A_MSIL1C_20190225T105021_N0207_R051_T31UDR_20190225T125616.SAFE/GRANULE/L1C_T31UDR_A019210_20190225T105315/IMG_DATA"
library(raster)
list.files(Dossier)
Bande1 <- raster(list.files(pattern = "\\B01.jp2$"))
Bande2 <- raster(list.files(pattern = "\\B02.jp2$"))
Bande3 <- raster(list.files(pattern = "\\B03.jp2$"))
Bande4 <- raster(list.files(pattern = "\\B04.jp2$"))
NDVI <- (Bande8-Bande4)/(Bande8+Bande4)
NDWI <- (Bande8A-Bande11)/(Bande8A-Bande11)
NDDI <- (NDVI-NDWII)/(NDVI+NDWII)
writeRaster(NDWI, "C:/Users/Perrin/Desktop/INRA/résultats R/NDWI.tif", overwrite = T)
writeRaster(NDVI, "C:/Users/Perrin/Desktop/INRA/résultats R/NDVI", overwrite = T)
writeRaster(NDDI, "C:/Users/Perrin/Desktop/INRA/résultats R/NDDI.tif", overwrite = T)
Any help will be very much appreciated.
Create a list with your objects and then use Map to loop over it:
rasterList <- list(NDVI = NDVI, NDWI = NDWI, NDDI = NDDI)
filenames<-sprintf("C:/Users/Perrin/Desktop/INRA/résultats R/%s_%s.tif",
names(rasterList), format(Sys.Date(), "%Y%m%d"))
Map(writeRaster, rasterList, filenames, MoreArgs = list(overwrite = TRUE))

Passing a character-element to render() to create several different PDFs

I try to create a number of PDFs with Markdown which all look the same, but use different data. For this purpose I created a little bit of code to generate random data which should be used in the creation of the PDF-File.
library(tidyverse)
r_sample <- seq(1, 5, by = 0.1)
nr_rows <- 10
df_names <- vector()
for(i in 1:2){
name <- paste0("i_",i)
i_sample <- tibble("Name" = paste0("i_", seq(1:nr_rows)))
i_sample <- i_sample %>% add_column(sample(r_sample, nr_rows, replace = TRUE)) %>%
rename("Value" = "sample(r_sample, nr_rows, replace = TRUE)")
assign(name, i_sample)
df_names[i] <- name
}
The Markdown-Script is almost empty, since it is just used for testing purposes.
---
title: "Test"
output: pdf_document
params:
data : ""
---
This is just a Test.
```{r echo = FALSE}
params$data
```. #error made intentionally!
Now I try to get the data from my R-Script into the Markdown-Script using render(). Since I want to loop over the different dataframes, I wrote a simple loop.
for(i in 1:2){
rmarkdown::render('Test.Rmd', params = list(data = df_names[i]),
output_file = glue::glue(paste0("file_", df_names[i], ".pdf")))
}
Now to the problem. Markdown understands the input of env_names[i] as a character and not as the dataframe. However if I type it manually like this:
rmarkdown::render('Test.Rmd', params = list(data = i_1), output_file = glue::glue(paste0("file_i_1.pdf")))
it works. I understand the problem, but I can't find a way to work around this.
If anyone could help I would be very glad! Thanks in advance.
The direct fix for the problem at hand is to wrap df_names[i] in get() in order to access the object whose name matches df_names[i] instead of passing the value of df_names[i] to render():
rmarkdown::render('Test.Rmd', params = list(data = get(df_names[i])),
output_file = glue::glue(paste0("file_", df_names[i], ".pdf")))
Yet, dynamically creating objects using assign in the global environment is rather unidiomatic in R – and not doing this also eradicates the need for using get() afterwards.
Therefore, I propose to write the code snippet from the question as follows:
r_sample <- seq(1, 5, by = 0.1)
nr_rows <- 10
df_list <- list()
for(i in 1:2){
df_list[[i]] <- tibble(Name = paste0("i_", seq(1:nr_rows)), Value = sample(r_sample, nr_rows, replace = TRUE))
}
Then, the loop render the documents becomes:
for(i in 1:2){
rmarkdown::render('Test.Rmd', params = list(data = df_list[[i]]),
output_file = paste0("file_i_", i, ".pdf"))
}

Save multiple plots as individual PDFs with unique names in R

I have a function that creates a plot and saves it as a pdf file. I plan on running this function in a loop for 100+ columns of a dataset. My question is how to make each pdf file uniquely named so it doesnt overwrite the previous file every time a new one is made. I tried to make it work with C integer format
pdf(file = "~/PTY/Data/ROC Curves/ROC%03d.pdf", onefile = F, width = 7, height = 7)
with the intended outcome of titling each plot ROC001, ROC002... but it still overwrites each new one as ROC001.
createROC <- function(x) {
ROCtable <- createNAtable(x) #create dataset w/out NAs
x_NA <-x[!is.na(x)] #create analyte vector w/out NAs
#create ROC variable
newROC <- rocit(score = x_NA, class = ROCtable$DSM, negref = "0", method = "non")
#create pdf
pdf(file = "~/PTY/Data/ROC Curves/ROC%03d.pdf", onefile = F, width = 7, height = 7)
#create ROC plot
plot(newROC, legend = F)
dev.off()
}
My function takes a vector (data1$IL6, data1$age, etc) as an input. Ideally, I would be able to title each plot after the input (IL6, age, etc).
Thank you ahead of time for your help
The problem is that you are opening and closing the graphics device with each plot, and the counter resets at 001 each time. You need one call to pdf(), followed by all of your calls to plot(), followed by dev.off(), like so:
# Some data with named columns
df <- data.frame(a = 1:10, b = 11:20, c = 21:30)
# A plotting function without `pdf()` and `dev.off()`
create_plot <- function(x, main) {
plot(x, main = main)
}
# Open graphics device
pdf(file = "filename-%03d.pdf", onefile = FALSE)
# Apply plotting function to each column of `df` in a loop
for (j in 1:3) {
create_plot(df[, j], names(df)[j])
}
# Close graphics device
dev.off()
This should generate filename-001.pdf, filename-002.pdf, and filename-003.pdf in your working directory.

How to print, write, or export princomp results to a csv file

Using princomp, results are not stored as a dataframe, e.g, running "summary(module_LS9)" is displayed in the console as table. What's the code to create a csv file or any table that I can format the output in Excel or Word?
I tried:
sink("summary_module_LS9.csv")
summary(module_LS9)
sink()
This does create a csv file, but the values are not separated by commas.
Tried also:
write.table(summary(module_LS9), file = "module_LS9_sum.csv", sep = ",", quote = FALSE, row.names = F)
Got:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ‘"summary.princomp"’ to a data.frame
Appreciate your time to help!
You could use any of these suggestions-
1.Use the package XLConnect which allows you to write R output to Excel files.
Here's an example, where I write a model and send the summary to excel:
library(XLConnect)
dat <- data.frame(rsp = rnorm(100, 0, 1),
pred1 = rnorm(100, 0, 1),
pred2 = rnorm(100, 0, 1))
model <- lm(rsp ~ pred1 + pred2, data = dat)
writeWorksheetToFile("model1.xlsx",
data = summary(dat),
sheet = "summary",
header = TRUE,
clearSheets = TRUE)
2.If you are only trying to export the output of summary function, try this
write.csv(summary(data_frame),"output.csv")
3. Use the package stargazer. It doesn't export output to Excel, but makes nice HTML, Latex and ASCII tables which can be copy pasted to Excel.It also allows to quickly create a table which compares different models.
More info: https://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf
Here is an as.data.frame method you can use:
as.data.frame.summary.princomp <- function(x, ...) {
vars <- x$sdev^2
vars <- vars/sum(vars)
type.convert(
as.data.frame(
rbind(`Standard deviation` = x$sdev, `Proportion of Variance` = vars,
`Cumulative Proportion` = cumsum(vars))
)
)
}
Call it like this:
res <- as.data.frame(summary(princomp(USArrests, cor = TRUE)))
# Comp.1 Comp.2 Comp.3 Comp.4
#Standard deviation 1.5748783 0.9948694 0.5971291 0.41644938
#Proportion of Variance 0.6200604 0.2474413 0.0891408 0.04335752
#Cumulative Proportion 0.6200604 0.8675017 0.9566425 1.00000000
A data.frame can be exported with write.csv as usual.
I can't find a way to save the summary, but you can obtain the same table saving a csv file of
comp<-princomp(base,cor=T)
var<-comp$sdev^2
write.csv(var,file="variance.csv")
Then go to the csv file, and calculate directly in excel the rest, is just the estandar desviation, proportion of variance from each one, and the cummulative proportion.

Do not collect results in a list in lapply [duplicate]

This question already has answers here:
Stop lapply from printing to console
(3 answers)
Closed 5 years ago.
Suppose I have a file structure defined as follows:
There are three folders: A, B and C. Each of the folders contains a file called file_demo.csv. Now I would like to read the file from each of the folders, do some operation on them and export them to three new files not in those folders. Subsequently I use lapply() to do.
Here's some code for a demo:
# the folder list
folder_list <- c('A', 'B', 'C')
# creating demo data frames
set.seed(1)
file_demo_a <- data.frame(X = rnorm(5),
Y = rpois(5, lambda = 2))
write_csv(file_demo_a, 'A/file_demo.csv')
set.seed(2)
file_demo_b <- data.frame(X = rnorm(5),
Y = rpois(5, lambda = 2))
write_csv(file_demo_b, 'B/file_demo.csv')
set.seed(3)
file_demo_c <- data.frame(X = rnorm(5),
Y = rpois(5, lambda = 2))
write_csv(file_demo_c, 'C/file_demo.csv')
# defining a function
df_mod_func <- function(folder_name){
path_name <- paste(folder_name, 'file_demo.csv', sep = "/")
new_demo <- read_csv(path_name)
new_demo <- new_demo + 1 # do a new operation
csv_file_name <- paste(folder_name, 'new_file_demo.csv', sep = "_")
new_demo %>% write_csv(csv_file_name)
# return(NULL)
}
lapply(folder_list, df_mod_func)
Now the problem I am facing is that when I call lapply(), each of the final data frames are printed to the console. This is a problem because these data files that I will load are huge and I do not want R to crash. I also do not want to store it a an object because of the huge size. I have also tried to return NULL in the function but that seems like a hacky way plus I do not want to fill up my console with useless output.
Is there a way to not get lapply (or use any other function) to collect the output in this case and just silently execute?
If it's just about not printing the result, you can always use invisible(), as in:
invisible( lapply( folder_list, df_mod_func ) )

Resources