Export kmeans clustering results to .csv - r

I've done a k-means clustering on my data, imported from .csv. Is there anyway to export the clustered results back to .csv file? Cos after the k-means clustering is done, the class of the variable is not a data frame but kmeans.

In most R package help files there will be a subheading that says "value" that describes the output from the analyses conducted. I have not used kmeans recently, but I believe you want something like this:
kmeansresults<-kmeans(dataframe)
x<-kmeansresults$clusters
write.csv(x, file="name_of_file.csv")

Related

How do I organize my data for time series cluster analysis using the dtwclust package?

I'm trying to do a hierarchical cluster dendrogram of time series in R using the dtwclust package. For my test dataset I have 4 columns with unequal lengths. the dtwclust package offers a way to equalize the lengths using a reinterpolate function. However I get this error when I try to use it with the following code
data <- reinterpolate(fshdtw, new.length = max(lengths(fshdtw)))
Error in check_consistency(x, "ts") : There are missing values in the series. This makes me think (I could be wrong) that the data table is not organized properly. Can anyone suggest how to 1. read in the data (I just imported it using RStudio) or any other way to resolve this issue. PS: I tried the analysis using the data provided in the package and it worked as advertised. Thanks in advance!
The data file is here https://www.dropbox.com/s/dih39ji0zop9xa0/fshdtw.txt?dl=0

R and Matlab Dendograms Structures

I know very little about R, but I need to convert the dendrogram resulted from hierarchical clustering in matlab into R dendrogram structure. The following table shows the dendrogram resulted from hierarchical clustering in matlab function; where the first and the second column are the IDs for the objects or branches, and the third column is the distance.
Is there a way to map this table (or matlab dendrogram) into R dendrogram?
I think that the easiest way for you to have a dendrogram in R is to use some intermediate results from your matlab analysis instead of using the final table.
Assuming that you have a dissimilarity matrix called Diss_Mat (which you should definitely evaluate at some point of your matlab algorithm), you could do the following
DIST_Mat=as.dist(Diss_Mat) #create a dist type object
dendro=as.dendrogram(hclust(DIST_Mat))
where with the second line you perform the hierarchical clustering in R and then you create a dendrogram type object.

Export Stata regression result to R?

My colleague and I chose Stata for regression analysis due to good handling of survey data. We also want to use R for graphics, but do not know how to export Stata regression model.
Within Stata, of course there is return list and ereturn list. But what's the best way to export these results outside of Stata? Direct to R would be ideal, but any intermediate format would be fine as well.
If I were to do this, I would create a Stata dataset with all the regression results using postfile. Then import it into R using, for example, some technique from here. Manipulate within R at your convenience.
See also the user-written command rsource: ssc describe rsource.
Disclaimer: my knowledge of R is rather limited.
I would advise using regsave
ssc install regsave
E.g. after a regression command you could use
regsave, tstat pval ci
to replace the current dataset by the estimation results and save it afterwards.

Imputation package "mi" output

I am using the "mi" package for imputation of missing values. I have run the following code:
'mi' package code
library(mi)
imp_rd<-mi(rd1) ## rd1 is my data file containing 7 variables.
summary(imp_rd)
hist(imp_rd)
Now, I want to save the output of
"imp_rd" (which is my imputed data file) as .csv file. Any one who will help me regarding this problem.
if you want to export imputed data-sets generated by the model that mi estimated, a good way to do it is by using the mi2stata command, which allows you to export to either a .dta or a .csv format.
But remember not to think about exporting "one" imputed data set. The whole point of multiple imputation is that you can get a bunch of different imputed data sets that will allow you to account for the uncertainty induced by the missing data that you originally had.
So be sure to specify how many imputed data sets you want to export and the path where you want to save the imputed data. In the following example I chose to generate 10 imputed data sets.
library(mi)
imp_rd<-mi(rd1)
mi2stata(imp_rd, m=10, "pathtofile/imp_rd.csv")
Hope you find this useful.
if your output file is a dataframe you can use:
write.csv(imp_rd, file = "imp_rd.csv", sep = ",")
this should save file in csv in your working directory
thanks

R 'longitudinal' package - exporting shrinkage objects

I am working with multivariate longitudinal data, and was hoping to use the 'longitudinal' package to look at dynamical correlation between variables. I have been able to run the code fine until the end:
[example using stored data from the package]:
library("longitudinal")
data(tcell)
dynpc <- dyn.pcor(tcell.34, lambda=0)
class(dynpc)
(object is of class"shrinkage")
So here is my problem: how do I export this data to a .txt or .csv file? The function gives me a matrix of correlations, along with other information.

Resources