Since learning MATLAB and later on R, my capability of improving working efficiency has greatly strengthened. But I believe that what I have used is just a tiny portion of it. The true power is, well, I don't know yet.
The core of programming is to identify patterns which are repeatable. Though everything differ from each other a little bit, by forming assumptions, certain things can be grouped together. So start with simple model first, then adding extra stuff to it to let it become more flexible and specific.
By learning writing R codes and turn it to be executable using ssh command lines, I should be able to modify my previous codes to design the matrix and generate summary using that.
I should also coded up the part of typing in a bunch of cases to put them in the mytbss folder. What I need in essence is just a list of scancode. I don't have to going back and forth to edit the directory every time. Just using the list of scancode, with task specifying (e.g. -m cp ), expected rename pattern (-n s/ns/whatever), I can get a txt file with all the cases to be copied, and what I need to do is just copy and paste and there we go!
So my focus is not spending to much time with that part. With the help of codes, I will be able to focus on more important stuff.
# Can't wait. I just draft it here.
tbsscp <- function(scancode,group) {
cbind(paste(group, scancode, ".nii", sep = ""))
}
scancode <- c("CCS001-1a11", "CCS002-1a22", "CCS003-1a33")
group <- "ns"
# Refresh: http://stackoverflow.com/questions/2470248/write-lines-of-text-to-a-file-in-r
tbsscplist <- file("tbsscplisttest.txt")
writeLines(tbsscp(scancode, group), tbsscplist)
close(tbsscplist)
2015年8月6日 星期四
8.6 I should probably start thinking about investing fitness
Standard Membership
• Access to Mission Bay or Parnassus fitness center
• Workout towel service
• Discounted parking: early morning, evening, and weekends
• Exclusive member activities
• Members-only pricing on massage, personal training, swim lessons, and other programs
• 10% discount at Sports Basement
Enrollment Fee: $50
Mission Bay: $52/month
Parnassus: $49/month
- See more at: http://www.campuslifeservices.ucsf.edu/fitnessrecreation/information/program_index/membership_ucsf_staff#sthash.NChIqh6O.dpuf
Oops. They thought I want some more information about the standard membership. I should have replied using my email address haha.
• Access to Mission Bay or Parnassus fitness center
• Workout towel service
• Discounted parking: early morning, evening, and weekends
• Exclusive member activities
• Members-only pricing on massage, personal training, swim lessons, and other programs
• 10% discount at Sports Basement
Enrollment Fee: $50
Mission Bay: $52/month
Parnassus: $49/month
- See more at: http://www.campuslifeservices.ucsf.edu/fitnessrecreation/information/program_index/membership_ucsf_staff#sthash.NChIqh6O.dpuf
Oops. They thought I want some more information about the standard membership. I should have replied using my email address haha.
8.6 Coursera Biostat Quiz 2 Question 6
Let X be a uniform random variable with support of length 1, but where we don't know where it starts. So that the density is
f(x)=1
for x∈(θ,θ+1) and 0 otherwise. We observe a random variable from this distribution, say x1 . What does the likelihood look like?
Discussion Forum posts:
Eric:
You don't need . I think of the likelyhood function () as a probability density function for the parameter given the data .
Often, you can use Bayes' theorem to go from to . (still hard to see how Bayes' theorem relate with this content) For this problem though, I think it just takes wrapping your head around the likelyhood function concept. Only one of the multiple choice answers makes sense.
I'll post the answer explanation on Monday, after the deadline.
Michelle:
You don't need $$F(x)$$. You can think of the ordinary density function $$f(x)$$ as actually a function of $$f(x,\theta)$$ (This is perfect, to look at the function with an extra parameter). Usually we treat $$\theta$$ as known and then ask what the density is at different values of $$x$$. A normal density plot has $$x$$ on the horizontal axis and $$f(x,\theta)$$ on the vertical axis, for a single, constant value of $$\theta$$ .
For this problem instead think of the plot with $$\theta$$ on the horizontal axis and $$f(x,\theta)$$ on the vertical axis, for a single constant value of $$x$$.
My approach was that I just assigned a value to x, such as $$x=2.5$$. Then I made a table with different values of $$\theta$$ in one column and then $$f(x,\theta)$$ in another column. I calculated half a dozen values, then made my plot of $$f(x,\theta)$$ vs. $$\theta$$.
- A horizontal line between x1 and x1 - 1
- A parabola
- A diagonal line from x1 to x1 + 1
- A point at x1
Discussion Forum posts:
Eric:
You don't need . I think of the likelyhood function () as a probability density function for the parameter given the data .
Often, you can use Bayes' theorem to go from to . (still hard to see how Bayes' theorem relate with this content) For this problem though, I think it just takes wrapping your head around the likelyhood function concept. Only one of the multiple choice answers makes sense.
I'll post the answer explanation on Monday, after the deadline.
Michelle:
You don't need $$F(x)$$. You can think of the ordinary density function $$f(x)$$ as actually a function of $$f(x,\theta)$$ (This is perfect, to look at the function with an extra parameter). Usually we treat $$\theta$$ as known and then ask what the density is at different values of $$x$$. A normal density plot has $$x$$ on the horizontal axis and $$f(x,\theta)$$ on the vertical axis, for a single, constant value of $$\theta$$ .
For this problem instead think of the plot with $$\theta$$ on the horizontal axis and $$f(x,\theta)$$ on the vertical axis, for a single constant value of $$x$$.
My approach was that I just assigned a value to x, such as $$x=2.5$$. Then I made a table with different values of $$\theta$$ in one column and then $$f(x,\theta)$$ in another column. I calculated half a dozen values, then made my plot of $$f(x,\theta)$$ vs. $$\theta$$.
2015年8月4日 星期二
8.4 Interweave in dataframe in R
# Interweave in dataframe
# Useful Link: http://stackoverflow.com/questions/14784861/interweave-two-data-frames-in-r
a = data.frame(x=1:5, y=5:1)
b = data.frame(x=2:6, y=4:0)
# Interweave rows
df <- rbind(data.frame(a, index = 1:nrow(a)), data.frame(b, index = 1:nrow(b)))
df <- df[order(df$index), c("x", "y")]
# Interweave columns
dfc <- cbind(rbind(a, 1:ncol(a)), rbind(b, 1:ncol(b)))
dfc[order(dfc[6,])]
# Try: interweave columns using names of the columns
dfc[order(names(dfc))] # This is faster and single line call!
# Useful Link: http://stackoverflow.com/questions/14784861/interweave-two-data-frames-in-r
a = data.frame(x=1:5, y=5:1)
b = data.frame(x=2:6, y=4:0)
# Interweave rows
df <- rbind(data.frame(a, index = 1:nrow(a)), data.frame(b, index = 1:nrow(b)))
df <- df[order(df$index), c("x", "y")]
# Interweave columns
dfc <- cbind(rbind(a, 1:ncol(a)), rbind(b, 1:ncol(b)))
dfc[order(dfc[6,])]
# Try: interweave columns using names of the columns
dfc[order(names(dfc))] # This is faster and single line call!
8.4 Compare two dataframes + Only focus on the differences
# Find elements that are not identical in two dataframes
# Useful link: http://stackoverflow.com/questions/3171426/compare-two-data-frames-to-find-the-rows-in-data-frame-1-that-are-not-present-in
library(xlsx)
library(proto)
library(sqldf)
library(compare)
x <- c(1,2,3,4,5)
y1 <- c(1,2,3,4,5)
y2 <- c(1,2,3,4,6)
d1 <- data.frame(x, y1)
d2 <- data.frame(x, y2)
compare(d1,d2)
# SQLDF solution
a1 <- data.frame(a = 1:5, b=letters[1:5])
a2 <- data.frame(a = 1:3, b=letters[1:3])
require(sqldf)
a1NotIna2 <- sqldf('SELECT * FROM a1 EXCEPT SELECT * FROM a2')
a1Ina2 <- sqldf('SELECT * FROM a1 INTERSECT SELECT * FROM a2')
# DPLYR solution
library(dplyr)
anti_join(a1,a2)
semi_join(a1,a2)
full_join(a1,a2)
(8.6 update) To only focus on the columns containing different elements instead of viewing a bunch of identical columns (such as ages, education, you know they will all be the same and less likely to be different), the trick is to add an extra row below the original dataframe which is a logical value, indicating if the two columns are identical. Can use idential() function in R to do that. Then, it is just a matter of dataframe subsetting.
Pretty cool. Huh?
# Useful link: http://stackoverflow.com/questions/3171426/compare-two-data-frames-to-find-the-rows-in-data-frame-1-that-are-not-present-in
library(xlsx)
library(proto)
library(sqldf)
library(compare)
x <- c(1,2,3,4,5)
y1 <- c(1,2,3,4,5)
y2 <- c(1,2,3,4,6)
d1 <- data.frame(x, y1)
d2 <- data.frame(x, y2)
compare(d1,d2)
# SQLDF solution
a1 <- data.frame(a = 1:5, b=letters[1:5])
a2 <- data.frame(a = 1:3, b=letters[1:3])
require(sqldf)
a1NotIna2 <- sqldf('SELECT * FROM a1 EXCEPT SELECT * FROM a2')
a1Ina2 <- sqldf('SELECT * FROM a1 INTERSECT SELECT * FROM a2')
# DPLYR solution
library(dplyr)
anti_join(a1,a2)
semi_join(a1,a2)
full_join(a1,a2)
(8.6 update) To only focus on the columns containing different elements instead of viewing a bunch of identical columns (such as ages, education, you know they will all be the same and less likely to be different), the trick is to add an extra row below the original dataframe which is a logical value, indicating if the two columns are identical. Can use idential() function in R to do that. Then, it is just a matter of dataframe subsetting.
Pretty cool. Huh?
2015年8月3日 星期一
8.3 Writing R that can be run in UNIX command lines
#! /usr/bin/env Rscript
# Useful website: http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script
options(echo=FALSE) # if you don't want to see commands in output file
args <- commandArgs(trailingOnly = TRUE)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandsArgs(trailingOnly=FALSE))
cat("Hello", args, "\n")
Return in UNIX:
# Useful website: http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script
options(echo=FALSE) # if you don't want to see commands in output file
args <- commandArgs(trailingOnly = TRUE)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandsArgs(trailingOnly=FALSE))
cat("Hello", args, "\n")
Return in UNIX:
>YukaideMacBook-Air:Desktop yukaizou$ ./script.R Kai
>Hello Kai
Alternatively,
>YukaideMacBook-Air:Desktop yukaizou$ Rscript script.R Kai
>Hello Kai
Another useful website here: http://www.cureffi.org/2014/01/15/running-r-batch-mode-linux/
It also shows how to use the optparse package to make options. That guy is a genius. I might follow the blog for some other materials.
# 8.6 update: learning how to add options
library(getopt)
library(optparse)
options(echo=FALSE) # if you don't want to see commands in output file
args <- commandArgs(trailingOnly = TRUE)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandsArgs(trailingOnly=FALSE))
option_list <- list(make_option(c("-a", "--avar"), action = "store", default = NA, type = "character",
help = "just a variable name"),
make_option(c("-v", "--verbose"), action = "store_true", default = TRUE,
help = "Should the program print the extra stuff out? [default %default]"),
make_option(c("-q", "--quiet"), action = "store_false", dest = "verbose",
help = "Make the program not verbose." )
)
opt <- parse_args(OptionParser(option_list = option_list))
if (opt$v) {
cat("Haha, you might think this is an verbose. Quiet me by adding a little -q!", "\n")
}
if (!is.na(opt$avar) ) {
cat("You have just typed in:", "\n")
cat(opt$avar, "\n")
} else {
cat("Please specify variable a", "\n")
}
Another useful website here: http://www.cureffi.org/2014/01/15/running-r-batch-mode-linux/
It also shows how to use the optparse package to make options. That guy is a genius. I might follow the blog for some other materials.
# 8.6 update: learning how to add options
library(getopt)
library(optparse)
options(echo=FALSE) # if you don't want to see commands in output file
args <- commandArgs(trailingOnly = TRUE)
# trailingOnly=TRUE means that only your arguments are returned, check:
# print(commandsArgs(trailingOnly=FALSE))
option_list <- list(make_option(c("-a", "--avar"), action = "store", default = NA, type = "character",
help = "just a variable name"),
make_option(c("-v", "--verbose"), action = "store_true", default = TRUE,
help = "Should the program print the extra stuff out? [default %default]"),
make_option(c("-q", "--quiet"), action = "store_false", dest = "verbose",
help = "Make the program not verbose." )
)
opt <- parse_args(OptionParser(option_list = option_list))
if (opt$v) {
cat("Haha, you might think this is an verbose. Quiet me by adding a little -q!", "\n")
}
if (!is.na(opt$avar) ) {
cat("You have just typed in:", "\n")
cat(opt$avar, "\n")
} else {
cat("Please specify variable a", "\n")
}
8.3 Such a low efficiency
It's August already.
Such a low efficiency now. It's 3 p.m.. What am I doing at VA. Waiting for a bunch of cases to be archived... Don't have motivations to do some other things.
Done Quiz 1, Quiz 2, and Project 1 of Exploratory Data Analysis, still working on Project 2 but that is ~40% complete. Have more time working on Regression Model and Biostat.
Such a low efficiency now. It's 3 p.m.. What am I doing at VA. Waiting for a bunch of cases to be archived... Don't have motivations to do some other things.
Done Quiz 1, Quiz 2, and Project 1 of Exploratory Data Analysis, still working on Project 2 but that is ~40% complete. Have more time working on Regression Model and Biostat.
訂閱:
文章 (Atom)