kernel = input_select(
c("Gaussian" = "gaussian",
"Epanechnikov" = "epanechnikov",
"Rectangular" = "rectangular",
"Triangular" = "triangular",
"Biweight" = "biweight",
"Cosine" = "cosine",
"Optcosine" = "optcosine"),
label = "Kernel")
)
plot(density(mtcars$wt)()
)
plot(density(mtcars$wt))
?density
mtcars %>% ggvis(x = ~wt) %>%
layer_densities(
adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
kernel = input_select(
c("Gaussian" = "gaussian",
"Epanechnikov" = "epanechnikov",
"Rectangular" = "rectangular",
"Triangular" = "triangular",
"Biweight" = "biweight",
"Cosine" = "cosine",
"Optcosine" = "optcosine"),
label = "Kernel")
)
?density
slider <- input_slider(10, 1000)
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points(size := slider) %>%
layer_points(fill := "red", opacity := 0.5, size := slider)
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points(size := input_slider(100, 1000, label = "black")) %>%
layer_points(fill := "red", size := input_slider(100, 1000, label = "red"))
install.packages("shiny")
install.packages("shiny")
install.packages("shiny")
install.packages("shiny")
install.packages("shiny")
library(shiny)
runExample("01_hello")
system.file("examples", package="shiny")
runExample("02_text") # tables and data frames
runExample("03_reactivity") # a reactive expression
runExample("04_mpg") # global variables
runExample("05_sliders") # slider bars
runExample("06_tabsets") # tabbed panels
runExample("07_widgets") # help text and submit buttons
runExample("08_html") # Shiny app built from HTML
runExample("09_upload") # file upload wizard
runExample("10_download") # file download wizard
runExample("11_timer") # an automated timer
install.packages('rsconnect')
rsconnect::setAccountInfo(name='jmchang', token='DACB25DC74D1BF9FF1AA08B685A47BE7', secret='9obozUnWEOoBd9SLwqfPZApNson4XEsD+rXMfFBA')
runApp("~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/helloWorld/")
library(rsconnect)
rsconnect::deployApp('~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/helloWorld/')
runApp('~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/ggvisExample/server.r/')
runApp('~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/ggvisExample/')
rsconnect::deployApp('~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/ggvisExample/')
rsconnect::deployApp('~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/ggvisExample/')
rsconnect::deployApp('~/Dropbox (jia-ming.chang)/13_NCCU/courses/資料科學實務_DataScienceInPractice/104.2/homeworks/homework3/ggvisExample/')
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points()
library(ggvis)
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points()
## Install necessary packages 
install.packages("devtools") 
library("devtools")
install.packages("ggvis")
# Load packages
library("ggvis")
## Install necessary packages
install.packages("devtools")
library("devtools")
install.packages("ggvis")
# Load packages
library("ggvis")
# Use mtcars data
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$am <- factor(mtcars$am)
hist.ggplot <- ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth=1)
library(ggplot)
library(ggplot2)
hist.ggplot <- ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth=1)
?ggplot
hist.ggplot
hist.ggvis <- mtcars %>% ggvis(x = ~mpg) %>% layer_histograms(width=1) %>% set_options(width = img.width, height = img.height)
img.width <- 450
img.height <- 300
img.width <- 450
img.width <- 450
img.height <- 300
hist.ggvis <- mtcars %>% ggvis(x = ~mpg) %>% layer_histograms(width=1) %>% set_options(width = img.width, height = img.height)
hist.ggvis
scatter.ggplot <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
scatter.ggplot
scatter.ggplot
scatter.ggvis <- mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_points() %>% set_options(width = img.width, height = img.height)
scatter.ggvis
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% layer_model_predictions(model = "lm", se = TRUE)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, main="Pie Chart of Countries")
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, main="Pie Chart of Countries")
data(iris)
head(iris, 3)
# log transform
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]
# apply PCA - scale. = TRUE is highly
# advisable, but default is FALSE.
ir.pca <- prcomp(log.ir,
center = TRUE,
scale. = TRUE)
summary(iris)
density(iris[,1:4])
density(iris[,1])
plot(density(iris[,1]))
plot(log(density(iris[,1]))
)
plot(log(density(iris[,1])))
plot((density(log(iris[,1])))
)
print(ir.pca)
plot(ir.pca, type = "l")
summary(summary)
summary(ir.pca)
?prcomp
1.7124583*1.7124583
0.9523797*0.9523797
# Predict PCs
predict(ir.pca,
newdata=tail(log.ir, 2))
library(devtools)
install_github("ggbiplot", "vqv")
library(ggbiplot)
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1,
groups = ir.species, ellipse = TRUE,
circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal',
legend.position = 'top')
print(g)
?ggbiplot
ir.pca
str(ir.pca)
ir.pca$rotation
ir.pca$rotation[1,]
ir.pca$rotation[1,]*ir.pca$rotation[1,]
sum(ir.pca$rotation[1,]*ir.pca$rotation[1,])
power(2)
power(2,3)
2^3
1.5*2^19
1.5*(2^19)
1524*512
2^32
1500*1024/2
d<-read.table("~/Downloads/grades.csv")
d<-read.table("~/Downloads/grade.txt")
head(d)
str(d)
d<-d[-1]
head(d)
d<-read.table("~/Downloads/grade.txt")
d[2:length((d))]
d$V1[-1]
str(d$V1[-1])
c<-d$V1[-1]
hist(d)
hist(as.numeric(d))
as.character(d)
as.character(c)
hist(as.numeric(as.character(c)))
hist(as.numeric(as.character(c)),main = "成績分布")
hist(as.numeric(as.character(c)),main = "Grade")
hist(as.numeric(as.character(c)),main = "Grade historm",x=)
hist(as.numeric(as.character(c)),main = "Grade Historm",x="Grade")
hist(as.numeric(as.character(c)),main = "Grade Historm",xlab = ="Grade")
hist(as.numeric(as.character(c)),main = "Grade Historm",xlab = "Grade")
?hist
hist(as.numeric(as.character(c)),main = "Grade Histograms",xlab = "Grade")
hist(as.numeric(as.character(c)),main = "Grade Histogram",xlab = "Grade")
data(iris)
head(iris, 3)

# log transform 
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]

# apply PCA - scale. = TRUE is highly 
# advisable, but default is FALSE. 
ir.pca <- prcomp(log.ir,
                 center = TRUE,
                 scale. = TRUE)
data(iris)
head(iris, 3)

# log transform 
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]

# apply PCA - scale. = TRUE is highly 
# advisable, but default is FALSE. 
ir.pca <- prcomp(log.ir,
                 center = TRUE,
                 scale. = TRUE)
data(iris)
data(iris)
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]
ir.pca <- prcomp(log.ir,
center = TRUE,
scale. = TRUE)
print(ir.pca)
ir.pca$rotation
t(ir.pca$rotation)
quit
quit()
protein <- read.table("~/Downloads/protein.txt", sep="\t", header=TRUE)
head(protein)
vars.to.use <- colnames(protein)[-1]
pmatrix <- scale(protein[,vars.to.use])
pcenter <- attr(pmatrix, "scaled:center")
pscale <- attr(pmatrix, "scaled:scale")
vars.to.use <- colnames(protein)[-1]
pmatrix <- scale(protein[,vars.to.use])
pcenter <- attr(pmatrix, "scaled:center")
pscale <- attr(pmatrix, "scaled:scale")
vars.to.use <- colnames(protein)[-1]
pmatrix <- scale(protein[,vars.to.use])
pcenter <- attr(pmatrix, "scaled:center")
pscale <- attr(pmatrix, "scaled:scale")
pmatrix
d <- dist(pmatrix, method="euclidean")
pfit <- hclust(d, method="ward")
plot(pfit, labels=protein$Country)
d <- dist(pmatrix, method="euclidean")
d <- dist(pmatrix, method="euclidean")
pfit <- hclust(d, method="ward")
pfit <- hclust(d, method="ward")
plot(pfit, labels=protein$Country)
rect.hclust(pfit, k=5)
rect.hclust(pfit, k=10)
plot(pfit, labels=protein$Country)
rect.hclust(pfit, k=10)
plot(pfit, labels=protein$Country)
rect.hclust(pfit, k=5)
?predict
rect.hclust(pfit, k=5)
groups <- cutree(pfit, k=5)
print_clusters <- function(labels, k) {
    for(i in 1:k) {
        print(paste("cluster", i))
        print(protein[labels==i,c("Country","RedMeat","Fish","Fr.Veg")])
    }
}
print_clusters <- function(labels, k) {
for(i in 1:k) {
print(paste("cluster", i))
print(protein[labels==i,c("Country","RedMeat","Fish","Fr.Veg")])
}
}
library(ggplot2)
princ <- prcomp(pmatrix)
nComp <- 2
project <- predict(princ, newdata=pmatrix)[,1:nComp]
project.plus <- cbind(as.data.frame(project),
cluster=as.factor(groups),
country=protein$Country)
project
head(project.plus)
ggplot(project.plus, aes(x=PC1, y=PC2)) +
    geom_point(aes(shape=cluster)) +
    geom_text(aes(label=country),
              hjust=0, vjust=1)
ggplot(project.plus, aes(x=PC1, y=PC2)) +
geom_point(aes(shape=cluster)) +
geom_text(aes(label=country),
hjust=0, vjust=1)
install.packages("e1071")
install.packages("klaR")
install.packages("tree")
install.packages("DAAG")
## svm
library(DAAG)
library(caret)
library(e1071)
library(rpart)
aqd <- read.csv("~/Downloads//mccdf_Guting4.csv", header = TRUE, sep = ",")
aqd1 <- aqd[ , c(3, 5:1420)]
newaqd1 <- na.omit(aqd1)
m1 <- svm(pm25~ PM25_Wanli_0 + sea_press_Banqiao_w_6 + sea_press_Banqiao_w_7 + sea_press_Banqiao_w_8
+ sea_press_Banqiao_w_9 + sea_press_Banqiao_w_11 + sea_press_Banqiao_w_12 + sea_press_Banqiao_w_17
+ sea_press_Banqiao_w_18 + sea_press_Banqiao_w_19 + sea_press_Banqiao_w_20 + sea_press_Banqiao_w_21
+ sea_press_Banqiao_w_22 + sea_press_Banqiao_w_23 + temerature_Banqiao_w_0 + temerature_Banqiao_w_1
+ temerature_Banqiao_w_2 + temerature_Banqiao_w_3 + temerature_Banqiao_w_4 + temerature_Banqiao_w_5
+ temerature_Banqiao_w_6 + temerature_Banqiao_w_7 + temerature_Banqiao_w_8 + temerature_Banqiao_w_9
+ temerature_Banqiao_w_22 +  temerature_Banqiao_w_23 +  dew_point_Banqiao_w_0 + dew_point_Banqiao_w_6
+  dew_point_Banqiao_w_7 + dew_point_Banqiao_w_8 +  dew_point_Banqiao_w_9 + dew_point_Banqiao_w_22
+ dew_point_Banqiao_w_23 + visb_Anbu_w_0 +  visb_Anbu_w_20 ,data = newaqd1)
pred = predict(m1, data = newaqd1)
newpred <- as.numeric(round(pred))
newaqd2 <- cbind(newaqd1, newpred)
cM <- table(newaqd2$newpred, newaqd2$pm25)
b <- confusionMatrix(cM)
str(cM)
?confusionMatrix
confusionMatrix(newaqd2$newpred, newaqd2$pm25)
str(newaqd2$newpred)
str(newaqd2$pm25)
cm<-confusionMatrix(as.factor(newaqd2$newpred),as.factor(newaqd2$pm25))
tmp<-as.factor(newaqd2$newpred)
str(tmp)
tmp<-as.factor(newaqd2$pm25)
str(tmp)
cM <- table(as.factor(newaqd2$newpred), as.factor(newaqd2$pm25))
str(cM)
a <- (cM[c(1:74),c(1:74)])
g <- confusionMatrix(a)
str(a)
q()
library("edgeR")
x <- read.delim("fileofcounts.txt",row.names="Symbol")
install.packages("corpcor")
q()
a<-c(1,2)
load("./RData")
load("~/.RData")
save.image("~/test.Rdata")
load("test.Rdata")
save.image("~/test.RData")
pam250 <- read.table("~/Dropbox/13_NCCU/courses/生物資訊概論與實務_Bioinformatics/1051/homeworks/assignment/pro2/PAM250.txt")
head(pam250)
str(pam250)
?print
?text
3^7
c(10,1)
choose(10,1)
choose(10,2)
choose(10,5)
1024-1-1-252
770/2
source("http://wush978.github.io/R/init-swirl.R")
rm(list=ls())
library(swirl);swirl()
skip()
chat()
submit()
?chol2inv
bye()
m <- matrix(c(1, 0, 0, 1), nrow = 2, ncol = 2, byrow = TRUE)
m
m2
mxm
m * m
m <- matrix(c(1, 0, 0, 0, 1, 0, 0,0, 0, 1, 0, 0, 0, 0, 1, 0), nrow = 4, ncol = 4, byrow = TRUE)
m
m * m
m <- matrix(c(1, 1, 0, 1, 0, 1, 1,0, 0, 0, 1, 1, 0, 0, 0, 1), nrow = 4, ncol = 4, byrow = TRUE)
m
m * m
m %*% m
m <- matrix(c(1, 0, 0, 0, 1, 0, 0,0, 0, 1, 0, 0, 0, 0, 1, 0), nrow = 4, ncol = 4, byrow = TRUE)
m %*% m
m %*% m %*% m
s <- matrix(c(0,1,0,0,0,0,1,0,0,0, 1,0,1,0,0,0,0,0,0,0, 0,1,0,1,0,0,0,1,0,0, 0,0,1,0,1,0,0,0,0,0, 0,0,0,1,0,1,0,0,0,0, 0,0,0,0,1,0,1,0,0,0, 1,0,0,0,0,1,0,0,0,1, 0,0,1,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,1,0,1, 0,0,0,0,0,0,1,0,1,0), nrow = 10, ncol = 10, byrow = TRUE)
s
s %*% s
s %*% s + s
power(2,3)
pow(2,3)
2^3
2^3 % 3
mod(2^3,2)
2^3 %% 2
2^3 %% 3
3^111 %% 3
(3^111) %% 3
(3^111)
(3^111) %% 143
(209^3) %% 53
(3^111) %% 143
(3^111) %% 11
(3^111) %% 143
(3^111) %% 11
getwd()
1+1
2^3
install.packages("clusterProfiler")
library("clusterProfiler")
install.packages('devtools')
library(devtools)
install_github('andreacirilloac/updateR')
library(updateR)
updateR()
# for loading rJava
dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
library(rJava)
library("clusterProfiler")
library('RDAVIDWebService')
diagonal
diagonal<-T
diagonal?"diagonal\n":"not diagonal\n"
?layout
?par
?plot.window
xlim <- c(-30,30)
ylim <- c(-5,5)
plot.new()
plot.window( xlim , ylim )
points(1,1)
points(0,0)
xlim <- c(-30,30)
ylim <- c(-5,5)
plot.window( xlim , ylim )
points(1,1)
points(0,0)
?rect
?polygon
n <- 100
xx <- c(0:n, n:0)
yy <- c(c(0, cumsum(stats::rnorm(n))), rev(c(0, cumsum(stats::rnorm(n)))))
plot   (xx, yy, type = "n", xlab = "Time", ylab = "Distance")
polygon(xx, yy, col = "gray", border = "red")
title("Distance Between Brownian Motions")
op <- par(mfrow = c(2, 1))
plot(c(1, 9), 1:2, type = "n")
op
polygon(1:9, c(2,1,2,1,1,2,1,2,1),
col = c("red", "blue"),
border = c("green", "yellow"),
lwd = 3, lty = c("dashed", "solid"))
par(op)
plot(c(1, 9), 1:2, type = "n")
polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
density = c(10, 20), angle = c(-45, 45))
max(seq(1:100))
?max
range(seq(1:10))
a<-range(seq(1:10))
ap2[]
a[2]
200000/160000
?range
1.1*10^7
1.1*10^7-10720000
160000*1.25
setwd("~/Dropbox/13_NCCU/courses/DataScienceInPractice_資料科學實務/1052/codes/codes17")
setwd("pums/")
load("psub.RData")
dtrain <- subset(psub,ORIGRANDGROUP >= 500)
dtest <- subset(psub,ORIGRANDGROUP < 500)  	# Note: 1
m1 <- step( 	# Note: 2
lm(log(PINCP,base=10) ~ AGEP + SEX + COW + SCHL,
data=dtrain), 	# Note: 3
direction='both')
rmse <- function(y, f) { sqrt(mean( (y-f)^2 )) } 	# Note: 4
print(rmse(log(dtest$PINCP,base=10),
predict(m1,newdata=dtest))) 	# Note: 5
# [1] 0.2752171
# Note 1:
#   Split data into test and training.
# Note 2:
#   Ask that the linear regression model we???re building be
#   stepwise improved, which is a powerful automated procedure for
#   removing variables that don???t seem to have significant impacts
#   (can improve generalization performance).
# Note 3:
#   Build the basic linear regression model.
# Note 4:
#   Define the RMSE function.
# Note 5:
#   Calculate the RMSE between the prediction and the
#   actuals.
phi <- function(x) { 	# Note: 1
x <- as.numeric(x)
c(x,x*x,combn(x,2,FUN=prod))
}
phiNames <- function(n) { 	# Note: 2
c(n,paste(n,n,sep=':'),
combn(n,2,FUN=function(x) {paste(x,collapse=':')}))
}
modelMatrix <- model.matrix(~ 0 + AGEP + SEX + COW + SCHL,psub) 	# Note: 3
colnames(modelMatrix) <- gsub('[^a-zA-Z0-9]+','_',
colnames(modelMatrix)) 	# Note: 4
modelMatrix
pM <- t(apply(modelMatrix,1,phi)) 	# Note: 5
colnames(modelMatrix) <- gsub('[^a-zA-Z0-9]+','_',
colnames(modelMatrix)) 	# Note: 4
pM <- t(apply(modelMatrix,1,phi)) 	# Note: 5
vars <- phiNames(colnames(modelMatrix))
vars <- gsub('[^a-zA-Z0-9]+','_',vars)
colnames(pM) <- vars 	# Note: 6
pM <- as.data.frame(pM)
head(pM)
?combn
combn(letters[1:4], 2)
formulaStr2 <- paste('log(PINCP,base=10)',
paste(vars,collapse=' + '),
sep=' ~ ')
m2 <- lm(as.formula(formulaStr2),data=pMtrain)
pM <- t(apply(modelMatrix,1,phi)) 	# Note: 5
vars <- phiNames(colnames(modelMatrix))
vars <- gsub('[^a-zA-Z0-9]+','_',vars)
colnames(pM) <- vars 	# Note: 6
pM <- as.data.frame(pM)
pM$PINCP <- psub$PINCP
pM$ORIGRANDGROUP <- psub$ORIGRANDGROUP
pMtrain <- subset(pM,ORIGRANDGROUP >= 500)
pMtest <- subset(pM,ORIGRANDGROUP < 500) 	# Note: 7
formulaStr2 <- paste('log(PINCP,base=10)',
paste(vars,collapse=' + '),
sep=' ~ ')
m2 <- lm(as.formula(formulaStr2),data=pMtrain)
coef2 <- summary(m2)$coefficients
interestingVars <- setdiff(rownames(coef2)[coef2[,'Pr(>|t|)']<0.01],
'(Intercept)')
head(coef2)
dim(coef2)
dim(summary(m1))
summary(m1)
dim(summary(m1)$coefficients)
17*16/2
interestingVars <- union(colnames(modelMatrix),interestingVars) 	# Note: 1
formulaStr3 <- paste('log(PINCP,base=10)',
paste(interestingVars,collapse=' + '),
sep=' ~ ')
m3 <- step(lm(as.formula(formulaStr3),data=pMtrain),direction='both') 	# Note: 2
print(rmse(log(pMtest$PINCP,base=10),predict(m3,newdata=pMtest))) 	# Note: 3
library('kernlab')
data('spirals')
sc <- specc(spirals, centers = 2)
s <- data.frame(x=spirals[,1],y=spirals[,2], class=as.factor(sc))
library('ggplot2')
ggplot(data=s) +
geom_text(aes(x=x,y=y,label=class,color=class)) +
coord_fixed() + theme_bw() + theme(legend.position='none')
install.packages("kernlab")
library('kernlab')
data('spirals')
sc <- specc(spirals, centers = 2)
s <- data.frame(x=spirals[,1],y=spirals[,2], class=as.factor(sc))
library('ggplot2')
ggplot(data=s) +
geom_text(aes(x=x,y=y,label=class,color=class)) +
coord_fixed() + theme_bw() + theme(legend.position='none')
