how to put regression results into a table in r - r

Im trying to take the output of the function summary(dataset.regression) and turn it into a table for a professional r markdown report to be shared via html. I have run a regression model on two variables in the dataset and would like to make a table of the p-value, std error and estimate and use kableExtra to make it look professional. However before getting to kablextra, is there an easier way to make this table than just entering the results manually in a table i.e extract the results I would like to make a table of from summary(dataset.regression)?
Thanks for the help.

You could do something like:
# Load the necessary packages
library(knitr)
library(kableExtra)
# Extract the p-values, standard errors, and estimates
p.values <- summary(dataset.regression)$p.value
std.errors <- summary(dataset.regression)$std.error
estimates <- summary(dataset.regression)$estimate
# Combine the extracted elements into a single matrix or data frame
results <- cbind(p.values, std.errors, estimates)
# Create a table in R Markdown using the kable function
kable(results, caption = "Regression Results") %>%
kable_styling(full_width = F)

Related

R function for displaying results of aov() and tukeyHSD() in R markdown in professional way

I have some data that's unfortunately PHI and I can't share (to make reproducible) but I'd like to display it in a PDF document from R markdown.
Currently my results will display, but they don't look great, i.e.:
I'd love to display the exact same numbers, but just "prettier". No "##" along the side, and just a nice table in R markdown
I've obviously googled it, and I ran across sites such as this one or this one
But there seems to be drawbacks to using many of these methods. Either it uses a different code to initially conduct the anova (I used aov(), some of these sites use lm() and it doesn't seem to work with aov() ), or it doesn't help with the Tukey results.... or when I load the package it hides an important function I need in a different package (library(papeR) masks summarise, and summarize from dplyr() ).
Is there a simply way I'm missing in order to display those results 'cleanly'?
I don't know how professionals format these things, but I think, the nicest solution is broom + any table package.
For example:
```{r warning = FALSE, echo = FALSE, message=FALSE}
library(broom)
library(flextable)
fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)
thsd <- TukeyHSD(fm1, "tension", ordered = TRUE)
xx <- tidy(thsd)
flextable(xx)
```
Looks nice, I think.

Exporting forecasting model and accuracy information from the "forecast" package in R

I have searched through the forum and has not been able to find any specific information that has been able to solve the below.
The main question is how to export (1) the model information (chosen model and parameters) from the forecast function, and (2) the accuracy information (MAPE, MSE etc.) from the generated forecast?
I am able to find the information just by calling the object forecast(retail)$model but not able to actually output the information column by column in the file. This has presumably something to do with the predefined matrix.
The only post I have been able to find is the folloing, but not been able to apply it onto my example.
Export accuracy of multiple timeseries forecasts in r into csv-document
I am working of the example from the post: https://robjhyndman.com/hyndsight/batch-forecasting/
The following code loops through in a for loop columns by columns and generates a statistical point forecast (mean object) based on the forecast() function and outputs to a column by column csv file.
library(forecast)
retail <- read.csv("https://robjhyndman.com/data/ausretail.csv",header=FALSE)
retail <- ts(retail[,-1],f=12,s=1982+3/12)
ns <- ncol(retail)
h <- 24
fcast <- matrix(NA,nrow=h,ncol=ns)
for(i in 1:ns)
fcast[,i] <- forecast(retail[,i],h=h)$mean
write(t(fcast),file="retailfcasts.csv",sep=",",ncol=ncol(fcast))
Hope someone is able to help out with refactoring the above code.
Highly appreciate all the help / guidance you can provide.
You can use the following code to successfully export a csv file.
After you have created your matrix, do this:
fcast <- as.data.frame(fcast)
write.table(fcast, file="retailfcasts.csv", quote=F, sep=";", dec=",", na="", row.names=T, col.names=T)
Is the solution you were looking for?

Creating a dendrogram with the results from the results the multipatt function in the indicspecies package

I am getting familiar with the multipatt function in indicspecies package. Thus far I only see summary being used to give a breakdown of the results. However I would like a dendrogram, ideally with the names of the species which are more 'indicative' of my given community location.
example from package file:
library(indicspecies)
library(stats)
data(wetland) ## Loads species data
wetkm = kmeans(wetland, centers=3) ## Creates three clusters using kmeans
## Runs the combination analysis using IndVal.g as statistic
wetpt = multipatt(wetland, wetkm$cluster, control = how(nperm=999))
## Lists those species with significant association to one combination
summary(wetpt)
wetpt gives the raw results but I am not sure how to proceed to get a cluster plot out of this result. Can anyone offer any pointers?

Variable Clustering (varclus) Summary Tables

I am using varclus from the Hmisc package in R. Are there ways to produce summary tables from varclus like those is in SAS (e.g. Output 100.1.2 and Output 100.1.3 ) in R. Basically, I would like to know the information that is contained in the plot in a tabular or matrix form. For example: what variables are in what clusters (in SAS cluster structure), proportion of variance they explain, etc.
# varclust example in R using mtcars data
mtc <- mtcars[,2:8]
mtcn <- data.matrix(mtc)
clust <- varclus(mtcn)
clust
plot(clust)
#cut_tree <- cutree(varclus(mtcn)$hclust, k=5) # This would show group membership, but only after I chose some a cut point, not what I am after

Looping through dataset variables in svy package

I've built an R function that uses the same explanatory variables on a range of columns. I've used the glm function, but now I need to do the same with svyglm from the survey package. The main problem I'm having is that I can't build loops by using svyglm(Data[,i]~explanatoryVariables) as I do in glm, because it doesn't like column names (which are however very practical in loops).
For example if you try
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
summary(svyglm(api00~ell+meals+mobility, design=dstrat))
everything is fine but if you want to loop through several dependent variables by using the column number (here 13), you get an error
summary(svyglm(apistrat[,13]~ell+meals+mobility,data=apistrat, design=dstrat))
Does anyone know how to get around this? To give a simple example (never mind the statistical accuracy or the link function) I need to achieve the equivalent of this in normal glm but using svyglm instead
for(i in (12:15)){
print(glm(apistrat[,i]~ ell+meals,data=apistrat)$aic)
}
You need to use as.formula to paste the appropriate columns for evaluation. I created a custom function for your case:
mysvy <- function(data, columns, ...) {
model <- lapply(as.list(columns), function(x) {
summary(svyglm(as.formula(paste0(names(data)[x], "~ell+meals+mobility")),
data = data, ...))
})
return(model)
}
Then you can run your your desired columns through the function.
# To run columns 13 - 15 and get the results into a list
results <- mysvy(apistrat, 13:15, design = dstrat)
# should return a list of 3. results[[1]] to see the first

Resources