安裝套件、資料讀入、輸出

教科書: Linear Models with R, 2nd, Julian J. Faraway

這本書裡的例子可以從 R 的套件 “faraway” 找到。

rm(list=ls()) #清空工作空間的變數
getwd() #查看目前工作路徑
## [1] "D:/course/112-1/regression model/markdown"
setwd("C:/Users/USER/Desktop/tmp") #設定工作路徑

#install.packages("faraway") #安裝套件
library("faraway") #使用套件,重開 R 時需要重新輸入
## Warning: 套件 'faraway' 是用 R 版本 4.3.1 來建造的
teengamb #資料 "teengamb"
##    sex status income verbal gamble
## 1    1     51   2.00      8   0.00
## 2    1     28   2.50      8   0.00
## 3    1     37   2.00      6   0.00
## 4    1     28   7.00      4   7.30
## 5    1     65   2.00      8  19.60
## 6    1     61   3.47      6   0.10
## 7    1     28   5.50      7   1.45
## 8    1     27   6.42      5   6.60
## 9    1     43   2.00      6   1.70
## 10   1     18   6.00      7   0.10
## 11   1     18   3.00      6   0.10
## 12   1     43   4.75      6   5.40
## 13   1     30   2.20      4   1.20
## 14   1     28   2.00      6   3.60
## 15   1     38   3.00      6   2.40
## 16   1     38   1.50      8   3.40
## 17   1     28   9.50      8   0.10
## 18   1     18  10.00      5   8.40
## 19   1     43   4.00      8  12.00
## 20   0     51   3.50      9   0.00
## 21   0     62   3.00      8   1.00
## 22   0     47   2.50      9   1.20
## 23   0     43   3.50      5   0.10
## 24   0     27  10.00      4 156.00
## 25   0     71   6.50      7  38.50
## 26   0     38   1.50      7   2.10
## 27   0     51   5.44      4  14.50
## 28   0     38   1.00      6   3.00
## 29   0     51   0.60      7   0.60
## 30   0     62   5.50      8   9.60
## 31   0     18  12.00      2  88.00
## 32   0     30   7.00      7  53.20
## 33   0     38  15.00      7  90.00
## 34   0     71   2.00     10   3.00
## 35   0     28   1.50      1  14.10
## 36   0     61   4.50      8  70.00
## 37   0     71   2.50      7  38.50
## 38   0     28   8.00      6  57.20
## 39   0     51  10.00      6   6.00
## 40   0     65   1.60      6  25.00
## 41   0     48   2.00      9   6.90
## 42   0     61  15.00      9  69.70
## 43   0     75   3.00      8  13.30
## 44   0     66   3.25      9   0.60
## 45   0     62   4.94      6  38.00
## 46   0     71   1.50      7  14.40
## 47   0     71   2.50      9  19.20
head(teengamb) #列出前六筆資料
##   sex status income verbal gamble
## 1   1     51   2.00      8    0.0
## 2   1     28   2.50      8    0.0
## 3   1     37   2.00      6    0.0
## 4   1     28   7.00      4    7.3
## 5   1     65   2.00      8   19.6
## 6   1     61   3.47      6    0.1
## 開啟 httpd 求助伺服器… 好了
write.table(teengamb,file="teengamb.txt", col.names=T, row.names = F)
write.csv(teengamb, file="teengamb.csv",row.names=F)
dir() #查看目前工作路徑裡有哪些檔案
## [1] "hw1.html"     "hw1.png"      "hw1.Rmd"      "R_basic.html" "R_basic.Rmd" 
## [6] "rsconnect"    "teengamb.csv" "teengamb.txt"
data1<-read.table(file="teengamb.txt",header = T)
head(data1)
##   sex status income verbal gamble
## 1   1     51   2.00      8    0.0
## 2   1     28   2.50      8    0.0
## 3   1     37   2.00      6    0.0
## 4   1     28   7.00      4    7.3
## 5   1     65   2.00      8   19.6
## 6   1     61   3.47      6    0.1
data2<-read.csv(file="teengamb.csv")
head(data2)
##   sex status income verbal gamble
## 1   1     51   2.00      8    0.0
## 2   1     28   2.50      8    0.0
## 3   1     37   2.00      6    0.0
## 4   1     28   7.00      4    7.3
## 5   1     65   2.00      8   19.6
## 6   1     61   3.47      6    0.1

值、向量

a <- 3
print(a)
## [1] 3
x <- c(1,3,5) 
x
## [1] 1 3 5
y <- 5:7; y
## [1] 5 6 7
a*x
## [1]  3  9 15
x + y - 1
## [1]  5  8 11
x*y
## [1]  5 18 35
max(y)
## [1] 7
which.max(y)
## [1] 3
mean(y)
## [1] 6
median(y)
## [1] 6

矩陣

z<-cbind(x,y); z # 3 by 2 matrix
##      x y
## [1,] 1 5
## [2,] 3 6
## [3,] 5 7
rbind(x,y)
##   [,1] [,2] [,3]
## x    1    3    5
## y    5    6    7
w<-matrix(1:10,nrow=2); w # 2 by 5 matrix
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
2*w - 1
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    5    9   13   17
## [2,]    3    7   11   15   19
z[,1]^2
## [1]  1  9 25
z[2,1]
## x 
## 3
t(z)
##   [,1] [,2] [,3]
## x    1    3    5
## y    5    6    7
A<-t(z) %*% z; A
##    x   y
## x 35  58
## y 58 110
solve(A) # A 的反矩陣
##            x           y
## x  0.2263374 -0.11934156
## y -0.1193416  0.07201646
colMeans(A)
##    x    y 
## 46.5 84.0

機率分配

?distribution 

runif(10,min = -1, max = 1) #生成 uniform(-1,1) 隨機樣本,樣本數 10
##  [1] -0.75349396  0.28288878 -0.57298575 -0.74096126  0.07211541  0.79651598
##  [7] -0.01407979  0.03985875 -0.96409464  0.29302377
pnorm(0, mean=1, sd=1) # Normal(1,1) CDF, F(x=0)
## [1] 0.1586553

資料型態

a<-3 #numeric
b<-"test" #chatacter
c<-c("Y","Y","N")
d<-matrix(1:6,ncol=2)
f<- data.frame(c,d) #資料框

class(a)
## [1] "numeric"
class(b)
## [1] "character"
class(c)
## [1] "character"
c2<-as.factor(c)
c2;class(c2)
## [1] Y Y N
## Levels: N Y
## [1] "factor"
f
##   c X1 X2
## 1 Y  1  4
## 2 Y  2  5
## 3 N  3  6
class(f)
## [1] "data.frame"
cbind(c,d) #文字和數字形成矩陣,統一變成文字
##      c          
## [1,] "Y" "1" "4"
## [2,] "Y" "2" "5"
## [3,] "N" "3" "6"