generated from Cal-Poly-Advanced-R/clust431
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
100 lines (82 loc) · 2.15 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# clust431
<!-- badges: start -->
<!-- badges: end -->
The goal of clust431 is to ...
## Installation
You can install the released version of clust431 from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("clust431")
```
## Example
This is a basic example which shows you how to solve a common problem:
```{r example}
library(clust431)
## basic example code
```
What is special about using `README.Rmd` instead of just `README.md`? You can include R chunks like so:
```{r cars}
iris2 = iris[1:4,]
em_clust = function(k, data){
ranmeans = rep(0, ncol(data))
for (i in 1:(k-1)){
ranmeans = rbind(ranmeans, ranmeans)
}
for (j in 1:k){
for(i in 1:ncol(data)){
maxval = max(data[,i])
minval = min(data[,i])
rand = runif(n=1, min=minval, max=maxval)
ranmeans[j,i] = rand
}
}
icluster = rep(0, nrow(data))
list = rep(0, k)
for (z in 1:nrow(data)){
for (y in 1:k){
list[y] = dmvnorm(data[z,], mean = ranmeans[y,], sigma = cov(data))
}
icluster[z] = which.max(list)
list = rep(0, k)
}
print(icluster)
means = rep(0,k)
data = cbind(data, icluster)
means = t(1:(ncol(data)-1))
for (i in 1:(k-1)){
means = rbind(means, means)
}
for (i in 1:k){
means[i,] = colMeans(data[data[,ncol(data)] == i,])[-ncol(data)]
}
print(means)
cluster = rep(0, nrow(data))
list = rep(0, k)
for (z in 1:nrow(data)){
for (y in 1:k){
list[y] = dmvnorm(data[z,-ncol(data)], mean = means[y,], sigma = cov(data[data[,ncol(data)] == y,-ncol(data)]))
}
cluster[z] = which.max(list)
list = rep(0, k)
}
return(cluster)
}
em_clust(k=2, data=iris2)
```
You'll still need to render `README.Rmd` regularly, to keep `README.md` up-to-date.
You can also embed plots, for example:
```{r pressure, echo = FALSE}
plot(pressure)
```
In that case, don't forget to commit and push the resulting figure files, so they display on GitHub!