Rlang(2)Diagram - Model - Loop - Ifelse
1. Generate the Diagram
This will draw the diagram for us.
> plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5", type = "l", ylim = c(0, 200))
> lines(x = pm$time, y = pm$h100, col = "red")
> legend(x = 15, y = 180, legend = c("8 m", "100 m"), col = c("black", "red"), lty = 1)
plot, lines, legend.
These command will open the file and draw the things on the file.
pdf(file ="/opt/data/output1.pdf")
plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5", type = "l", ylim = c(0, 200))
lines(x = pm$time, y = pm$h100, col = "red")
legend(x = 15, y = 180, legend = c("8 m", "100 m"), col = c("black", "red"), lty = 1)
dev.off()
Some complex draw example
plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5 at 8 m", type = "l", ylim = c(0, 200), axes=FALSE)
axis(2)
axis(4)
axis(1, at = 0 : 23, labels = 0 : 23)
points(x = pm$time, y = pm$h100, col = "red", type = "l")
points(x = pm$time, y = pm$h325, col = "blue", type = "l")
abline(h = c(10, 15 , 25, 35), col = "grey", lty = 2)
legend("top", legend = c("8 m", "100 m", "325 m"), col = c("red", "black", "blue"), lty = 1)
box() #
Some function I need to be familiar with. plot, box plot, points(), lines(), lines(), abline(), box(), axis(), legend(), pdf()
2. Modeling
Functions: lm(), nls(), par()
Linear Regression
https://zh.wikipedia.org/zh-cn/%E7%B7%9A%E6%80%A7%E5%9B%9E%E6%AD%B8
http://www.jasongj.com/2015/03/27/ml1_linear_regression/
loss function, error function, cost function
Draw the 4 Plot
mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
pm <- read.csv(file = mydata)
m <- lm(pm$h100 ~ pm$h8)
summary(m)
par(mfrow = c(2,2))
plot(m)
Draw the Line and All the Points
mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
pm <- read.csv(file = mydata)
m <- lm(pm$h100 ~ pm$h8)
summary(m)
par(mfrow = c(1, 1))
plot(pm$h8, pm$h100, cex = 2, pch = 21, bg = "red", col = "green")
abline(m, col = "purple", lwd = 3)
legend("bottomright", pch = c(21, NA), lty = c(NA, 1), legend = c("Data", "Linear fit"),
pt.bg = "red", col = c("green", "purple"), lwd = c(NA, 2))
3. Loop
Draw three plots, based on the different high of the data
> par(mfrow=c(1,3))
> plot(pm[,1], pm[,2], cex=2, type = "l")
> plot(pm[,1], pm[,3], cex=2, type = "l")
> plot(pm[,1], pm[,4], cex=2, type = "l")
The for Loop
par(mfrow=c(1,3))
for (i in c(2, 3, 4)) plot(pm[, 1], pm[, i], cex = 2, type = "l")
Some other useful functions are : for(), while(), until()
() for function, [] for the index of array or seq, {} for code block.
4. Check Statements
[1] FALSE
> 3>2 | 1 > 2
[1] TRUE
> 3>2&!(1>2)
[1] TRUE
That is really powerful
> x <- 1:3
> x == 2
[1] FALSE TRUE FALSE
Define 2 array of numbers
> x <- 6:1
> x
[1] 6 5 4 3 2 1
> y <-c(3,5,7)
> y
[1] 3 5 7
Check if x is in y
> x %in% y
[1] FALSE TRUE FALSE TRUE FALSE FALSE
Find the number in x and also in y
> x[x %in% y]
[1] 5 3
Find their index
> which(x %in% y)
[1] 2 4
Find the max value exist in which time
> mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
> pm <- read.csv(file = mydata)
> pm$time[which(pm$h8 == max(pm$h8))]
[1] 8 9 10
Print is also a useful command in R language
> x <- 12
> if(x<99) print("x is less than 99")
[1] "x is less than 99"
Useful Command ifelse
> x <- 12
> if (x < 99) {
+ print("x is less than 99")
+ } else {
+ print("x is larger than 99")
+ }
[1] "x is less than 99"
> ifelse(x < 99, "small", "large")
[1] "small"
IFELSE can be powerful used in a lot of place
plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5",
cex = ifelse(pm$h8 == max(pm$h8), 2, 1),
pch = 16, type = "b",
col = ifelse(pm$h8 > 75, "red","darkgreen"))
CEX may be related to how big the points will be.
References:
http://dapengde.com/archives/14824
http://dapengde.com/archives/14833
http://dapengde.com/archives/14836
http://dapengde.com/archives/14843
Rlang(2)Diagram - Model - Loop - Ifelse
猜你喜欢
转载自sillycat.iteye.com/blog/2240395
今日推荐
周排行