AdSense

Saturday, July 6, 2019

R: Logistic Regression


0_runme.txt

########## R: Logistic Regression

##### Run this script on your R Console

# Set your working directory on your R Console
# The following directory is dummy - set to your own directory where you save all the r files below.
setwd('/Users/yoshi/Dropbox/Google Drive/Coding/R/logistic_regression/')

#source('1_install_packages.r') # You have to run this r script only for the first time.

source('2_library.r')

source('3_data.r')

source('4_logistic_regression.r')

#Reference
#https://www.datacamp.com/community/tutorials/logistic-regression-R



Source: https://qiita.com/katsu1110/items/e4ef613559f02f183af5



1_install_packages.r
########## install packages

install.packages("ISLR")
#install.packages('ISLR_1.2.tgz')
#tgz file dowloaded from
#https://cran.r-project.org/web/packages/ISLR/index.html

install.packages("Amelia")
#install.packages('Amelia_1.7.5.tgz')
#tgz file dowloaded from
#https://cran.r-project.org/web/packages/Amelia/index.html

install.packages("mlbench")
#install.packages('mlbench_2.1-1.tgz')
#tgz file dowloaded from
#https://cran.r-project.org/web/packages/mlbench/

install.packages("corrplot")
#install.packages('corrplot_0.84.tgz')
#tgz file dowloaded from
#https://cran.r-project.org/web/packages/corrplot/

install.packages("caret")
#install.packages('caret_6.0-84.tgz')
#tgz file dowloaded from
#https://cran.r-project.org/web/packages/caret/


2_library.r
########## library setting

library('ISLR')

library(Amelia)
library(mlbench)

library(corrplot)

library("caret")


3_data.r
#For this tutorial, you're going to work with the Smarket dataset within RStudio.
# The dataset shows daily percentage returns for the S&P 500 stock index between 2001 and 2005.


names(Smarket)
#[1] "Year"      "Lag1"      "Lag2"      "Lag3"      "Lag4"      "Lag5"      "Volume"    "Today"     "Direction"

head(Smarket)
#  Year   Lag1   Lag2   Lag3   Lag4   Lag5 Volume  Today Direction
#1 2001  0.381 -0.192 -2.624 -1.055  5.010 1.1913  0.959        Up
#2 2001  0.959  0.381 -0.192 -2.624 -1.055 1.2965  1.032        Up
#3 2001  1.032  0.959  0.381 -0.192 -2.624 1.4112 -0.623      Down
#4 2001 -0.623  1.032  0.959  0.381 -0.192 1.2760  0.614        Up
#5 2001  0.614 -0.623  1.032  0.959  0.381 1.2057  0.213        Up
#6 2001  0.213  0.614 -0.623  1.032  0.959 1.3491  1.392        Up


summary(Smarket)
#      Year           Lag1                Lag2                Lag3                Lag4                Lag5              Volume           Today           Direction
# Min.   :2001   Min.   :-4.922000   Min.   :-4.922000   Min.   :-4.922000   Min.   :-4.922000   Min.   :-4.92200   Min.   :0.3561   Min.   :-4.922000   Down:602
# 1st Qu.:2002   1st Qu.:-0.639500   1st Qu.:-0.639500   1st Qu.:-0.640000   1st Qu.:-0.640000   1st Qu.:-0.64000   1st Qu.:1.2574   1st Qu.:-0.639500   Up  :648
# Median :2003   Median : 0.039000   Median : 0.039000   Median : 0.038500   Median : 0.038500   Median : 0.03850   Median :1.4229   Median : 0.038500            
# Mean   :2003   Mean   : 0.003834   Mean   : 0.003919   Mean   : 0.001716   Mean   : 0.001636   Mean   : 0.00561   Mean   :1.4783   Mean   : 0.003138            
# 3rd Qu.:2004   3rd Qu.: 0.596750   3rd Qu.: 0.596750   3rd Qu.: 0.596750   3rd Qu.: 0.596750   3rd Qu.: 0.59700   3rd Qu.:1.6417   3rd Qu.: 0.596750            
# Max.   :2005   Max.   : 5.733000   Max.   : 5.733000   Max.   : 5.733000   Max.   : 5.733000   Max.   : 5.73300   Max.   :3.1525   Max.   : 5.733000            


# Response vairable: Direction - that shows whether the market went up or down since the previous day.



# Visualizing Data
#
# histogram

##### Start: saving as a png file
png("fig_3_data_1.png", width = 1600, height = 1600)

par(mfrow=c(1,8))
for(i in 1:8) {
    hist(Smarket[,i], main=names(Smarket)[i])
}

dev.off()
##### End: saving as a png file

# It's extremely hard to see, but most of the variables show a Gaussian or double Gaussian distribution.



##### Start: saving as a png file
png("fig_3_data_2.png", width = 1600, height = 1600)

par(mfrow=c(1,8))
for(i in 1:8) {
    boxplot(Smarket[,i], main=names(Smarket)[i])
}

dev.off()
##### End: saving as a png file



##### Start: saving as a png file
#png("fig_3_data_3.png", width = 1600, height = 1600)
png("fig_3_data_3.png")

missmap(Smarket, col=c("blue", "red"), legend=FALSE)

dev.off()
##### End: saving as a png file


##### Start: saving as a png file
#png("fig_3_data_4.png", width = 1600, height = 1600)
png("fig_3_data_4.png")

correlations <- cor(Smarket[,1:8])
corrplot(correlations, method="circle")

dev.off()
##### End: saving as a png file


##### Start: saving as a png file
#png("fig_3_data_5.png", width = 1600, height = 1600)
png("fig_3_data_5.png")

pairs(Smarket, col=Smarket$Direction)

dev.off()
##### End: saving as a png file


##### Start: saving as a png file
#png("fig_3_data_6.png", width = 1600, height = 1600)
png("fig_3_data_6.png")

x <- Smarket[,1:8]
y <- Smarket[,9]
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)

dev.off()
##### End: saving as a png file



4_logistic_regression.r
########## Logistics Regression


##### Building Logistic Regression Model

glm.fit <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, data = Smarket, family = binomial)

summary(glm.fit)

# You look at the first 5 probabilities and they are very close to 50%:
glm.probs <- predict(glm.fit,type = "response")
glm.probs[1:5]

glm.pred <- ifelse(glm.probs > 0.5, "Up", "Down")


attach(Smarket)
table(glm.pred,Direction)

mean(glm.pred == Direction)



##### Creating Training and Test Samples

# Make training and test set
train = Year<2005
glm.fit <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume,
               data = Smarket,
               family = binomial,
               subset = train)

glm.probs <- predict(glm.fit,
                    newdata = Smarket[!train,],
                    type = "response")

glm.pred <- ifelse(glm.probs > 0.5, "Up", "Down")


Direction.2005 = Smarket$Direction[!train]
table(glm.pred, Direction.2005)

##         Direction.2005
## glm.pred Down Up
##     Down   77 97
##     Up     34 44

mean(glm.pred == Direction.2005)

## [1] 0.4801587



##### Solving Overfitting

# Fit a smaller model
glm.fit = glm(Direction ~ Lag1 + Lag2 + Lag3, data = Smarket, family = binomial, subset = train)
glm.probs = predict(glm.fit, newdata = Smarket[!train,], type = "response")
glm.pred = ifelse(glm.probs > 0.5, "Up", "Down")
table(glm.pred, Direction.2005)

##         Direction.2005
## glm.pred Down  Up
##     Down   39  31
##     Up     72 110

mean(glm.pred == Direction.2005)

## [1] 0.5912698



summary(glm.fit)

##
## Call:
## glm(formula = Direction ~ Lag1 + Lag2 + Lag3, family = binomial,
##     data = Smarket, subset = train)
##
## Deviance Residuals:
##    Min      1Q  Median      3Q     Max
## -1.338  -1.189   1.072   1.163   1.335
##
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.032230   0.063377   0.509    0.611
## Lag1        -0.055523   0.051709  -1.074    0.283
## Lag2        -0.044300   0.051674  -0.857    0.391
## Lag3         0.008815   0.051495   0.171    0.864
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 1383.3  on 997  degrees of freedom
## Residual deviance: 1381.4  on 994  degrees of freedom
## AIC: 1389.4
##
## Number of Fisher Scoring iterations: 3



R: Regularization and Logistics Regression


0_runme.txt

########## R: L1 Regularization for Logistic Regression

##### Run this script on your R Console


##### Background
#
# We use machine learning models to learn training data.
# The trained machine learning models are expected to predict in a reliable manner even when using new data (which is different from the training data above).
# If the machined learning model is over-fitting the training data (including noises and outlier),
# the model's prediction accuracy for new data could be lowered.
# This is because the model learn noises, outliers, and other meaningful data points of the training data, and regard the entire data as meaningful.
# To explain noises and outliers, the model is overly optimized.
#
# Reasons for over-fitting are mainly (1) numbers of data points are too small, (2) too many explanatory variables, and (3) too big parameters (coefficients).
#
# To avoid over-fitting, we can use regularization. This method is widely used in various machine learning models.
#
# Regularization: A way to find a model while avoiding over-fitting
#     L1 (Lasso): A penalty term is sum of absolute parameter values of the model
#                 By setting weight = 0 of certain data, deleting unnecessary data.
#                 "Dimension comperession to delete unnecessary explanatory variables"
#     L2 (Ridge): A penalty term is sum of squared parameter values of the model.
#                 This is to have a smoother model.
#                 "More accurate prediction while avoiding over-fitting"
# Under both L1 regularization and L2 regularization,
# models with lower dimensions have smaller penalty.
# If training data have exceptional data such as noises and outliers,
# models have to increase its dimensions to explain data including such exceptional data
# while trying not to be penalized for increased dimensions.
# (Both L1 and L2 can be simultaneously used as liner sum. This is elastic net regularization.)
#
#
# Regression:
#     A certain objective variable Y is predicted by using weighted explanatory variables X {x0, x1, x2, ..., xn}
#     Predicted Y = hθ(X) = θ0 * x0 + θ1 * x1 + ... + θn * xn =θT X
#
# Logistic regression:
#     Generally, hθ(X) above is a continuous value without any upper and lower boundaries.
#     To make 0 ≤ hθ(X) ≤ 1,
#     Logistic Function (AKA Sigmoid Function) g(z) = 1/(1 + e^(−z))
#     When doing logistic regressions,
#     hθ(X) = 1/(1 + e^(−θT X))
#
#     hθ(x)≥0.5, then Y = 1
#     hθ(x)<0.5, then Y = 0


# Set your working directory on your R Console
# The following directory is dummy - set to your own directory where you save all the r files below.
setwd('/Users/yoshi/Dropbox/Google Drive/Coding/R/regularization_and_logistic_regression/')

# source('1_install_packages.r') # You have to run this r script only for the first time.

source('2_library.r')

source('3_quick_start.r')

source('4_logistic_regression_binominal.r')

source('5_logistic_regression_multinominal.r')



Source: https://qiita.com/katsu1110/items/e4ef613559f02f183af5



1_install_packages.r
########## install packages

install.packages("glmnet")
#install.packages('glmnet_2.0-18.tgz')
#zip file dowloaded from https://cran.r-project.org/web/packages/glmnet/index.html

# Reference
#http://web.stanford.edu/~hastie/glmnet/glmnet_alpha.html



# Dowload files
#
# QuickStartExample.RData
# https://github.com/cran/glmnet/blob/master/data/QuickStartExample.RData
#
# BinomialExample.RData
# https://github.com/cran/glmnet/blob/master/data/BinomialExample.RData
#
# MultinomialExample.RData
# https://github.com/cran/glmnet/blob/master/data/MultinomialExample.RData



2_library.r
########## library setting

library('glmnet')



3_quick_start.r
# Dowload QuickStartExample.RData from the follwowing Github site.
# https://github.com/cran/glmnet/blob/master/data/QuickStartExample.RData

load("QuickStartExample.RData")

fit = glmnet(x, y)


##### Start: saving as a png file
png("fig_3_quick_start_1.png")

#draw a figure
plot(fit)
# Each curve corresponds to a variable.
# It shows the path of its coefficient against the ℓ1-norm of the whole coefficient vector at as λ varies.
# (The tuning parameter λ controls the overall strength of the penalty.)

dev.off()
##### End: saving as a png file


#A summary of the glmnet path at each step is displayed if we just enter the object name or use the print function:
print(fit)


coef(fit,s=0.1)


nx = matrix(rnorm(10*20),10,20)
predict(fit,newx=nx,s=c(0.1,0.05))


cvfit = cv.glmnet(x, y)



##### Start: saving as a png file
png("fig_3_quick_start_2.png")

plot(cvfit)

dev.off()
##### End: saving as a png file


cvfit$lambda.min


coef(cvfit, s = "lambda.min")


predict(cvfit, newx = x[1:5,], s = "lambda.min")


4_logistic_regression_binominal.r
# Download from https://github.com/cran/glmnet/blob/master/data/BinomialExample.RData
load("BinomialExample.RData")

fit = glmnet(x, y, family = "binomial")


##### Start: saving as a png file
png("fig_4_logistic_regression_binominal_1.png")

plot(fit, xvar = "dev", label = TRUE)

dev.off()
##### End: saving as a png file


predict(fit, newx = x[1:5,], type = "class", s = c(0.05, 0.01))


cvfit = cv.glmnet(x, y, family = "binomial", type.measure = "class")


##### Start: saving as a png file
png("fig_4_logistic_regression_binominal_2.png")

plot(cvfit)

dev.off()
##### End: saving as a png file


cvfit$lambda.min


cvfit$lambda.1se


coef(cvfit, s = "lambda.min")


predict(cvfit, newx = x[1:10,], s = "lambda.min", type = "class")





5_logistic_regression_multinominal.r
# Download from https://github.com/cran/glmnet/blob/master/data/MultinomialExample.RData
load("MultinomialExample.RData")


fit = glmnet(x, y, family = "multinomial", type.multinomial = "grouped")

##### Start: saving as a png file
png("fig_5_logistic_regression_multinominal_1.png")

plot(fit, xvar = "lambda", label = TRUE, type.coef = "2norm")

dev.off()
##### End: saving as a png file


cvfit=cv.glmnet(x, y, family="multinomial", type.multinomial = "grouped", parallel = TRUE)

##### Start: saving as a png file
png("fig_5_logistic_regression_multinominal_2.png")

plot(cvfit)

dev.off()
##### End: saving as a png file

predict(cvfit, newx = x[1:10,], s = "lambda.min", type = "class")





Figures






Friday, June 28, 2019

R: linear and polynominal regressions - finding an optimal order of polynominal models


0_runme.txt

########## R: linear and polynominal regressions - finding an optimal order of polynominal models

# Run this script on your R Console

# Set your working directory on your R Console
# The following directory is dummy - set to your own directory where you save all the r files below.
#setwd('/Users/XXX/Dropbox/Google Drive/Coding/R/polynomial_regression/')

# source('1_install_packages.r') # You have to run this r script only for the first time.

source('2_library.r')

source('3_data_plotting.r')

source('4_linear_regression.r')

source('5_polynomial_regression.r')

source('6_anova.r')



1_install_packages.r
########## install packages

install.packages('labeling')
#install.packages('labeling_0.3.zip')      #zip file dowloaded from https://cran.r-project.org/web/packages/labeling/index.html

install.packages('withr')
#install.packages('withr_2.1.2.zip')       #zip file dowloaded from https://cran.r-project.org/web/packages/withr/index.html

install.packages('pkgconfig')
#install.packages('pkgconfig_2.0.2.zip')                #zip file dowloaded from https://cran.r-project.org/web/packages/pkgconfig/index.html

install.packages('zeallot')
#install.packages('zeallot_0.1.0.zip')     #zip file dowloaded from https://cran.r-project.org/web/packages/zeallot/index.html

install.packages('vctrs')
#install.packages('vctrs_0.1.0.zip')        #zip file dowloaded from https://cran.r-project.org/web/packages/vctrs/index.html

install.packages('crayon')
#install.packages('crayon_1.3.4.zip')    #zip file dowloaded from https://cran.r-project.org/web/packages/crayon/index.html

install.packages('pillar')
#install.packages('pillar_1.4.1.zip')       #zip file dowloaded from https://cran.r-project.org/web/packages/pillar/index.html

install.packages('tibble')
#install.packages('tibble_2.1.3.zip')      #zip file dowloaded from https://cran.r-project.org/web/packages/tibble/index.html

install.packages('lazyeval')
#install.packages('lazyeval_0.2.2.zip')  #zip file dowloaded from https://cran.r-project.org/web/packages/lazyeval/index.html

install.packages('colorspace')
#install.packages('colorspace_1.4-1.zip')              #zip file dowloaded from https://cran.r-project.org/web/packages/colorspace/index.html

install.packages('munsell')
#install.packages('munsell_0.5.0.zip')  #zip file dowloaded from https://cran.r-project.org/web/packages/munsell/index.html

install.packages('Rccp')
#install.packages('Rcpp_1.0.1.zip')        #zip file dowloaded from https://cran.r-project.org/web/packages/Rcpp/index.html

install.packages('scales')
#install.packages('scales_1.0.0.zip')      #zip file dowloaded from https://cran.r-project.org/web/packages/scales/index.html

install.packages('rlang')
#install.packages('rlang_0.4.0.zip')       #zip file dowloaded from https://cran.r-project.org/web/packages/rlang/index.html

install.packages(‘gtable’)
#install.packages('gtable_0.3.0.zip')     #zip file dowloaded from https://cran.r-project.org/web/packages/gtable/index.html

install.packages('ggplot2')
#install.packages('ggplot2_3.2.0.zip')   #zip file dowloaded https://cloud.r-project.org/web/packages/ggplot2/index.html


2_library.r
########## library setting

library('labeling')
library('withr')
library('pkgconfig')
library('zeallot')
library('vctrs')
library('crayon')
library('pillar')
library('tibble')
library('lazyeval')
library('colorspace')
library('munsell')
library('Rcpp')
library('scales')
library('rlang')
library('gtable')
library('ggplot2')


3_data_plotting.r
########## data plotting


gp3 <- ggplot(cars, aes(x = speed, y = dist)) + geom_point(size = 3) + xlim(0,25)
gp3


#draw a figure
print(gp3)


#saving as a png file
ggsave(file = "fig_3_data_plotting.png", plot = gp3)

4_linear_regression.r
########## linear regression
#
#If speed = 0, then dist = 0 in this case, so intercept should be zero
lm1 <- lm(dist ~ speed - 1, cars)           # - 1 to make the intercept zero
summary(lm1)

#Call:
#lm(formula = dist ~ speed - 1, data = cars)
#
#Residuals:
#    Min      1Q  Median      3Q     Max
#-26.183 -12.637  -5.455   4.590  50.181
#
#Coefficients:
#      Estimate Std. Error t value Pr(>|t|)
#speed   2.9091     0.1414   20.58   <2e-16 ***
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 16.26 on 49 degrees of freedom
#Multiple R-squared:  0.8963,    Adjusted R-squared:  0.8942
#F-statistic: 423.5 on 1 and 49 DF,  p-value: < 2.2e-16
#

#coefficient of speed: 2.9091 (dist increases by 2.9091 when speed +1 mph)
#coefficient of speed (std. error): +-0.1414
#p-value is small enough. <2e-16 ***  It is considered to be significant.
#Residual Standard Error 16.26 < (dist average = 42.98), 16.26 is 37% of 42.98
#R-squared:  0.8942 (~1)

gp4 <- gp3 + geom_smooth(method = lm, formula = y ~ x - 1, se = FALSE, fullrange = TRUE)


#draw a figure
print(gp4)


#saving as a png file
ggsave(file = "fig_4_linear_regression.png", plot = gp4)


5_polynomial_regression.r
########## second (2) order polynomial regression

# degree = 2
# raw = TRUE to add "-1" to the model
lm2 <- lm(dist ~ poly(speed, degree = 2, raw = TRUE) - 1, cars)
summary(lm2)

gp5_2 <- gp3 + geom_smooth(method = lm, formula = y ~ poly(x, degree = 2, raw = TRUE) - 1, se = FALSE, fullrange = TRUE)


#draw a figure
print(gp5_2)

#saving as a png file
ggsave(file = "fig_5_polynomial_regression_2.png", plot = gp5_2)


#Call:
#lm(formula = dist ~ poly(speed, degree = 2, raw = TRUE) - 1,
#    data = cars)
#
#Residuals:
#    Min      1Q  Median      3Q     Max
#-28.836  -9.071  -3.152   4.570  44.986
#
#Coefficients:
#                                     Estimate Std. Error t value Pr(>|t|)
#poly(speed, degree = 2, raw = TRUE)1  1.23903    0.55997   2.213  0.03171 *
#poly(speed, degree = 2, raw = TRUE)2  0.09014    0.02939   3.067  0.00355 **
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 15.02 on 48 degrees of freedom
#Multiple R-squared:  0.9133,    Adjusted R-squared:  0.9097
#F-statistic: 252.8 on 2 and 48 DF,  p-value: < 2.2e-16

#Residual standard error: 15.02 < 16.26 (previous result)



########## third (3) order polynomial regression
lm3 <- lm(dist ~ poly(speed, degree = 3, raw = TRUE) - 1, cars)
summary(lm3)
gp5_3 <- gp3 + geom_smooth(method = lm, formula = y ~ poly(x, degree = 3, raw = TRUE) - 1, se = FALSE, fullrange = TRUE)

#draw a figure
print(gp5_3)

#saving as a png file
ggsave(file = "fig_5_polynomial_regression_3.png", plot = gp5_3)



########## fifth (5) order polynomial regression
lm5 <- lm(dist ~ poly(speed, degree = 5, raw = TRUE) - 1, cars)
summary(lm5)
gp5_5 <- gp3 + geom_smooth(method = lm, formula = y ~ poly(x, degree = 5, raw = TRUE) - 1, se = FALSE, fullrange = TRUE)

#draw a figure
print(gp5_5)

#saving as a png file
ggsave(file = "fig_5_polynomial_regression_5.png", plot = gp5_5)



########## ninth (9) order polynomial regression
lm9 <- lm(dist ~ poly(speed, degree = 9, raw = TRUE) - 1, cars)
summary(lm9)
gp5_9 <- gp3 + geom_smooth(method = lm, formula = y ~ poly(x, degree = 9, raw = TRUE) - 1, se = FALSE, fullrange = TRUE)

#draw a figure
print(gp5_9)

#saving as a png file
ggsave(file = "fig_5_polynomial_regression_9.png", plot = gp5_9)


#Residual standard error is even smaller, but p-value is now bigger.
#This is like overfitting; too complicated models are not necessarily good.

6_anova.r
########## ANOVA: analysis of variance "So, how can we optimize the number of orders?"

#Option A (the simplest): finding larger number of orders with significant p-value

#Option B (ANOVA: analysis of variance): finding larger number of orders with significant p-value
# We can use ANOVA since regressions here are "nested" - second order polynominal regression is a liner regression plus another term.

capture.output(anova(lm1, lm2, lm3, lm5, lm9), file = "output_6_anova.txt")


#If you run anova on your R Console, then you can use sink() as follows:
#
#sink("output_6_anova.txt") # sink: save results of the following anova command.
#anova(lm1, lm2, lm3, lm5, lm9)
#sink() # This second sink() is to stop saving results.



#Analysis of Variance Table
#
#Model 1: dist ~ speed - 1
#Model 2: dist ~ poly(speed, degree = 2, raw = TRUE) - 1
#Model 3: dist ~ poly(speed, degree = 3, raw = TRUE) - 1
#Model 4: dist ~ poly(speed, degree = 5, raw = TRUE) - 1
#Model 5: dist ~ poly(speed, degree = 9, raw = TRUE) - 1
#  Res.Df   RSS Df Sum of Sq      F   Pr(>F)
#1     49 12954                            
#2     48 10831  1   2122.66 9.1542 0.004272 **
#3     47 10743  1     87.75 0.3784 0.541845
#4     45 10263  2    480.05 1.0351 0.364269
#5     41  9507  4    756.35 0.8155 0.522711
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1

# If adding a variable makes the model have higher explanatory power,
# then you can see ** within Pr(>F) as shown above.
# 0.004272 **
#
# So, in this case, second order polynominal model
#Model 2: dist ~ poly(speed, degree = 2, raw = TRUE) - 1
# is the one we should choose.
#
# We do not go into detail, but this result (degree = 2) is backed by physics.



After running r scripts above, you'll get these png files on your R Console working directory:







After running 6_anova.r above, you'll get a txt file (output_6_anova.txt) on your R Console working directory which include:


Analysis of Variance Table

Model 1: dist ~ speed - 1
Model 2: dist ~ poly(speed, degree = 2, raw = TRUE) - 1
Model 3: dist ~ poly(speed, degree = 3, raw = TRUE) - 1
Model 4: dist ~ poly(speed, degree = 5, raw = TRUE) - 1
Model 5: dist ~ poly(speed, degree = 9, raw = TRUE) - 1
  Res.Df   RSS Df Sum of Sq      F   Pr(>F)
1     49 12954                              
2     48 10831  1   2122.66 9.1542 0.004272 **
3     47 10743  1     87.75 0.3784 0.541845
4     45 10263  2    480.05 1.0351 0.364269
5     41  9507  4    756.35 0.8155 0.522711
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


Monday, June 17, 2019

keeping minutes of a meeting

A small tip for keeping minutes of a meeting:

1. If you join a meeting, then please try to take minutes of the meeting by yourself (unless you’re a final decision maker). If you cannot understand words, contexts, etc., ask for other participants’ help after the meeting. (That’s why taking minutes is one of the best ways to learn especially for young professionals.)

2. Finish taking your own minutes, ask other participants (on your end, such as your boss and colleagues) to review, update the minutes when necessary.

3. After completing the minutes on your company’s end, then distribute it to all the participants and other related parties. When sending the completed minutes, please do include executive summary not only for those who cannot attend, but also for the participants who join the meeting. Executive summary is the best way to summarize what needs to be done, how, when, who.. If you really understand the situation, you should be able to do it; otherwise, you understand you did not understand it.


Hope this helps.

Friday, May 17, 2019

Vicious spirals of personal life and professional career

Vicious spirals of personal life and professional career

  1. Insufficient effort
  2. Poor results
  3. Buck-passing
  4. Slipping into victim mentality
  5. Trying to escape stress caused by the mentality above
  6. Becoming a victimizer (and being punished because of the unethical and/or illegal activities)

Tuesday, April 30, 2019

My own beliefs

My own beliefs: If you have nothing constructive, productive, or meaningful, then say nothing. If you have strong esteem needs, then go to Facebook and/or Instagram. (By the way, I like cool things on social networks. So please don’t get me wrong.)

Monday, February 11, 2019

actual cause and effect relation

Do distinguish the difference between an actual cause and effect relation and a simple correlation (without a cause and effect). To do so, think about a possibility of (1) just a coincidence, (2) a relationship based on another (or other) variable(s), and (3) an inverse cause and effect (cause and effect are the other way around).

(2) A confounding factor could affect both (seeming) cause and effect simultaneously; in that case, the deceptive cause and effect actually do not have the relationship.

To avoid (1), (2), and (3), counterfactual thinking is an important approach although a counter-fact cannot be observed; it has to be estimated.

Deep Learning (Regression, Multiple Features/Explanatory Variables, Supervised Learning): Impelementation and Showing Biases and Weights

Deep Learning (Regression, Multiple Features/Explanatory Variables, Supervised Learning): Impelementation and Showing Biases and Weights ...