setwd("L:/Data/Teaching/MyCourse/105-1_Programming/Code")
install.packages( "mice" )		 # 安裝 mice 軟體套件
library( mice )			 # 載入 mice 軟體套件
data( nhanes2 )
nrow( nhanes2 )			# nhanes2 資料集的橫列數
ncol( nhanes2 ) 			# nhanes2 資料集的直行數
summary( nhanes2 )			# nhanes2 資料集的概括資訊
head( nhanes2 )
nhanes2[ , 4 ]
nhanes2[ , 4 ]
sub <- which( is.na( nhanes2[ , 4 ] ) == TRUE )
subNA <- which( is.na( nhanes2[ , 4 ] ) == TRUE )
dataOK <- nhanes2[ -subNA, ]
dataOK
dataNA <- nhanes2[  subNA, ]
dataNA
subNA
subNA <- which( is.na( nhanes2[ , 4 ] ) == TRUE subNAbmi <- which( is.na( nhanes2[ , 2 ] ) == TRUE )
)
subNAbmi <- which( is.na( nhanes2[ , 2 ] ) == TRUE )
subNAbmi
subNAchl
subNAchl <- which( is.na( nhanes2[ , 4 ] ) == TRUE )
subNAchl
?or
?&
?and
and
??and
?!
and
?|
subNAchl | subNAbmi
subNA <- which( is.na( nhanes2[ , 4 ] ) == TRUE | is.na( nhanes2[ , 2 ] ) == TRUE )
subNA
dataOK <- nhanes2[ -subNA, ]
dataOK
dataNA <- nhanes2[  subNA, ]
dataNA
nhanes2
subNA <- which( is.na( nhanes2[ , 4 ] ) == TRUE | is.na( nhanes2[ , 2 ] ) == TRUE )
dataOK <- nhanes2[ -subNA, ]
dataNA <- nhanes2[  subNA, ]
dataOK
dataNA
plot( dataOK[,2], dataOK[,4])
lmout <- lm( chl ~ bmi, data = dataOK )
lmout
plot( dataOK[,2], dataOK[,4])
?abline
abline( lmout )
abline( a = 87.130, b = 3.963 )
plot( dataOK[,2], dataOK[,4])
abline( a = 87.130, b = 3.963 )
?plot
?xlim
abline( a = 87.130, b = 3.963, xlim = c(0,50) )
plot( dataOK[,2], dataOK[,4], xlim = c(0,50))
plot( dataOK[,2], dataOK[,4], xlim = c(0,40))
abline( a = 87.130, b = 3.963  )
plot( dataOK[,2], dataOK[,4], xlim = c(0,40), ylim = c(0,300))
abline( a = 87.130, b = 3.963  )
abline( a = 87.130, b = 3.963, col = "red"  )
plot( dataOK[ ,2 ], dataOK[ ,4 ], xlim = c(0,40), ylim = c(0,400 )
abline( a = 87.130, b = 3.963, col = "red" )
plot( dataOK[ ,2 ], dataOK[ ,4 ], xlim = c(0,40), ylim = c(0,400 ) )
abline( a = 87.130, b = 3.963, col = "red" )
abline( lmout, col = "green" )
car
cars
plot(car)
plot(cars)
dataPredict <- round( predict( lmout, dataNA ) )
dataPredict
dataNA[,4] <- round( predict( lmout, dataNA ) )
dataNA
nhanes2[ , 4 ] 					# 針對第4組數據
sub <- which( is.na( nhanes2[ , 4 ] ) == TRUE )
dataTR <- nhanes2[ -sub, ]
dataTE <- nhanes2[  sub, ]
dataTE
lmout <- lm( chl ~ age, data = dataTR )
lmout
dataTE[ , 4 ] <- round( predict( lmout, dataTE ) )
dataTE
dataTE
nhanes2[ , 4 ] 					# 針對第4組數據
sub <- which( is.na( nhanes2[ , 4 ] ) == TRUE )
dataTR <- nhanes2[ -sub, ]
dataTE <- nhanes2[  sub, ]
dataTE
dataTE
dataTE[ , 4 ] <- round( predict( lmout, dataTE ) )
dataTE
subNA <- which( is.na( nhanes2[ , 4 ] ) == TRUE | is.na( nhanes2[ , 2 ] ) == TRUE )
dataOK <- nhanes2[ -subNA, ]
dataNA <- nhanes2[  subNA, ]
dataOK
dataNA
lmout <- lm( chl ~ bmi, data = dataOK )
lmout
dataNA
dataNA[ , 4 ] <- round( predict( lmout, dataNA ) )
dataNA
dataOK
nhanes2
data0 <- ndata0 <- nhanes2
data0 <- nhanes2
data0
cars
summary( cars )
plot( cars[ , 1 ], cars[ , 2 ], xlim = c(0, 30 ), ylim = c(0, 130 ) )
?hnanes2
?nhanes2
lmchlbmi <- lm( chl ~ bmi, data = dataOK )
lmchlbmi
lmcars <- lm( speed ~ dist, data = cars )
lmcars
plot( cars[ , 1 ], cars[ , 2 ], xlim = c(0, 30 ), ylim = c(0, 130 ) )
abline( a = 0.1656, b = 8.2839, col = "red" )
abline( a = 8.2839, b = 0.1656, col = "red" )
lmcars <- lm( speed ~ dist, data = cars )
abline( lmcars, col = "green" )
lmcars <- lm( dist ~ speed, data = cars )
lmcars
abline( lmcars, col = "green" )
abline( a = -17.579, b = 3.932, col = "red" )
plot( cars[ , 1 ], cars[ , 2 ], xlim = c(0, 30 ), ylim = c(0, 130 ) )
lmcars <- lm( dist ~ speed, data = cars )
abline( a = -17.579, b = 3.932, col = "red" )
abline( lmcars, col = "green" )
abline( lmcars, col = "green" )
?lty
??lty
abline( lmcars, col = "green", lwd = 10 )
plot( dataOK[ , 2 ], dataOK[ , 4 ], xlim = c( 0, 40 ), ylim = c( 0, 400 ) )
abline( a = 87.130, b = 3.963, col = "red", lwd = 10 )
abline( lmchlbmi, col = "green" )
plot( dataOK[ , 2 ], dataOK[ , 4 ], xlim = c( 0, 40 ), ylim = c( 0, 400 ) )
abline( a = 87.130, b = 3.963, col = "red", lwd = 5 )
abline( lmchlbmi, col = "green" )
plot( dataOK[ , 2 ], dataOK[ , 4 ], xlim = c( 0, 40 ), ylim = c( 0, 400 ) )
abline( a = 87.130, b = 3.963, col = "red", lwd = 5 )
abline( lmchlbmi, col = "green", lwd = 3" )
plot( dataOK[ , 2 ], dataOK[ , 4 ], xlim = c( 0, 40 ), ylim = c( 0, 400 ) )
abline( a = 87.130, b = 3.963, col = "red", lwd = 5 )
abline( lmchlbmi, col = "green", lwd = 3" )
abline( lmchlbmi, col = "green", lwd = 3" )
abline( lmchlbmi, col = "green", lwd = 3 )
abline( lmchlbmi, col = "green", lwd = 3" )
abline( lmchlbmi, col = "green", lwd = 3 )
plot( dataOK[ , 2 ], dataOK[ , 4 ], xlim = c( 0, 40 ), ylim = c( 0, 400 ) )
abline( a = 87.130, b = 3.963, col = "red", lwd = 8 )
abline( lmchlbmi, col = "green", lwd = 3 )
plot( dataOK[ , 2 ], dataOK[ , 4 ], xlim = c( 0, 40 ), ylim = c( 0, 400 ) )
abline( a = 87.130, b = 3.963, col = "red", lwd = 8 )
abline( lmchlbmi, col = "green", lwd = 4 )
plot( cars[ , 1 ], cars[ , 2 ], xlim = c(0, 30 ), ylim = c(0, 130 ) )
lmcars <- lm( dist ~ speed, data = cars )
abline( a = -17.579, b = 3.932, col = "red", lwd = 8 )
abline( lmcars, col = "green", lwd = 4 )
plot( cars[ , 1 ], cars[ , 2 ], xlim = c(0, 30 ), ylim = c(-20, 130 ) )
lmcars <- lm( dist ~ speed, data = cars )
abline( a = -17.579, b = 3.932, col = "red", lwd = 8 )
abline( lmcars, col = "green", lwd = 4 )
plot( cars[ , 1 ], cars[ , 2 ], xlim = c(0, 30 ), ylim = c(-20, 130 ) )
imp <- mice( nhanes2, m = 4 )	# 產生四組完整的資料庫
fit <- with ( imp, lm( chl ~ age + hyp + bmi ) )
fit
?mice
imp <- mice( nhanes2, m = 2 )
imp
fit <- with ( imp, lm( chl ~ age + hyp + bmi ) )
fit
fit <- with ( imp, lm( chl ~ hyp + bmi ) )
fit
?plot
?plot3
?plot3d
??plot3d
plot3D
install.packages("plot3D")
?plot3D
library( plot3D )
?plot3D
example(scatterplot3d)
?scatter3D
z <- seq(0, 10, 0.2)
x <- cos(z)
y <- sin(z)*z
# greyish background for the boxtype (bty = "g")
scatter3D(x, y, z, phi = 0, bty = "g",
pch = 20, cex = 2, ticktype = "detailed")
chl
data0
data0[,4]
chl <- data0[,4]
hyp <- data0[,3]
bmi <- data0[,2]
bmi
chl
hyp
fit
pooled <- pool( fit ) 			# 對四組模型進行整理
summary( pooled )
scater3D( chl, hyp, bmi)
scatter3D( chl, hyp, bmi)
data0
iris
summary(iris)
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( -2, 10 ), ylim = c(-2, 10 ) )
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris1 <- lm( Sepal.Length ~ Sepal.Width, data = iris )
lmiris1
abline( a = 6.5262, b = -0.2234, col = "red", lwd = 8 )
lmiris1 <- lm( Sepal.Length ~ Sepal.Width, data = iris )
abline( a = 6.5262, b = -0.2234, col = "red", lwd = 8 )
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris1 <- lm( Sepal.Length ~ Sepal.Width, data = iris )
abline( a = 6.5262, b = -0.2234, col = "red", lwd = 8 )
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris1 <- lm( Sepal.Length ~ Sepal.Width, data = iris )
abline( a = 6.5262, b = -0.2234, col = "red", lwd = 8 )
abline( lmiris1, col = "green", lwd = 4 )
summary(iris)
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris1 <- lm( Sepal.Width ~ Sepal.Length, data = iris )
abline( a = 6.5262, b = -0.2234, col = "red", lwd = 8 )
abline( lmiris1, col = "green", lwd = 4 )
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris1 <- lm( Sepal.Width ~ Sepal.Length, data = iris )
abline( a = 6.5262, b = -0.2234, col = "red", lwd = 8 )
lmiris1
abline( a = 3.41895, b = -0.06188 , col = "red", lwd = 8 )
abline( lmiris1, col = "green", lwd = 4 )
plot( iris[ , 1 ], iris[ , 2 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris1 <- lm( Sepal.Width ~ Sepal.Length, data = iris )
abline( a = 3.41895, b = -0.06188 , col = "red", lwd = 8 )
abline( lmiris1, col = "green", lwd = 4 )
summary(iris)
plot( iris[ , 3 ], iris[ , 4 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris2 <- lm( Petal.Width ~ Petal.Length, data = iris )
lmiris2
abline( a = -0.3631, b = 0.4158, col = "red", lwd = 8 )
abline( lmiris1, col = "green", lwd = 4 )
plot( iris[ , 3 ], iris[ , 4 ], xlim = c( 0, 8 ), ylim = c( 0, 5 ) )
lmiris2 <- lm( Petal.Width ~ Petal.Length, data = iris )
abline( a = -0.3631, b = 0.4158, col = "red", lwd = 8 )
abline( lmiris2, col = "green", lwd = 4 )
?cor
cor( iris[, 1:4], use = "pairwise")
cor_iris <- cor( iris[, 1:4], use = "pairwise")
cor_iris
install.packages( "ellipse" )
library( ellipse )
plotcorr( cor_iris, col = rep( c( "white", "black" ), 5 ))
plotcorr( cor_iris, type = "lower", col = rep( c( "white", "black" ), 5 ))
plotcorr( cor_iris, col = rep( c( "white", "black" ), 5 ))
plotcorr( cor_iris, col = rep( c( "blue", "red" ), 5 ))
plotcorr( cor_iris, col = rep( c( "blue", "red" ), 5 ), , type = "lower" )
plotcorr( cor_iris, col = rep( c( "blue", "red" ), 5 ) )
plotcorr( cor_iris, col = c( "blue", "red", "green" ) )
plotcorr( cor_iris, col = c( "blue", "red", "green", "yellow" ) )
savehistory("L:/Data/Teaching/MyCourse/105-1_Programming/Code/data_170101_Linear.RHistory")
