#Convertng: As - Numeric (Nama Data) As - Character (Nama Data)
#Convertng: As - Numeric (Nama Data) As - Character (Nama Data)
#SELECTING
#MEMBERI NAMA KOLOM SUATU DATA
Colnames(namadata)<-c(“nama1”,”nama2)
#ANALISIS DESKRIPTIF
#UKURAN PEMUSATAN
mean(swiss$Fertility)
mean(InsectSprays$count[InsectSprays$spray=="kategori"]) #rata-rata dengan kategori tertentu
median(swiss$Fertility)
fer<-table(swiss$Fertility)
fer
modusfer<-names(fer)[which.max(fer)]
modusfer
rowSums(state.x77) #jumlah perkolom
colSums(state.x77) #jumlah perbaris
colMeans(state.x77) #mean perkolom
#UKURAN PENYEBARAN
sd(swiss$Fertility)
var(swiss$Fertility)
sd(swiss$Education)
cv_fer<-(sd(swiss$Fertility)/mean(swiss$Fertility))*100 #koefisien variasi
cv_ed<- (sd(swiss$Education)/mean(swiss$Education))*100
print(c(cv_fer, cv_ed))
#QUARTIL
quantile(swiss$Fertility, 0.25) #25% memiliki angka fertilitas
quantile(swiss$Fertility, 0.5)
quantile(swiss$Fertility, 0.75)
quantile (swiss$Fertility, c(0.25,0.5,0.75))
#VISUALISASI DATA
#SCATTER PLOT
plot(swiss)
#MENAMBAH JUDUL DAN WARNA (WARNA, NAMA X, NAMAY, NAMA GRAFIK)
plot(swiss$Fertility,swiss$Education,col="orange",xlab="fertility",ylab="education", main="Scatter
Plot Between Fertility and Education",)
plot(swiss$Fertility,swiss$Education,col="orange",xlab="fertility",ylab="education", main="Scatter
Plot Between Fertility and Education",pch=5)
#MEMBUAT HISTOGRAM
hist(swiss$Fertility,xlab="fretility", ylab="frekuensi", col="red", main="hitogram")
#sumbu y nya adalah peluang
hist(swiss$Fertility,xlab="fretility", ylab="probability", col="red", main="hitogram",probability = T,
ylim=c(0,0.05))
#membuat garis poligon
lines(density(swiss$Fertility),lwd=5)
#MEMBUAT BOXPLOT
#otomatis ada labelnya tiap boxplot
boxplot(state.x77[1:3], xlab="variabel", ylab="Rate",col="green", main="Boxplot of Fertility")
#hanya satu diwakili xlab
boxplot(state.x77$Murder, xlab="Murder", ylab="Rate",col="green", main="Boxplot of Fertility")
#Jika label terpisah kolom, kolom digabung dulu, otomatis ternamai
gabung<-cbind(Income=state.x77$Income,Illiteracy=state.x77$Illiteracy)
boxplot(gabung, xlab=”Variabel”, ylab="Rate",col="green", main="Boxplot of Fertility")
#Jika suatu variabel dijadikan kategorik, maka otomatis jika diplot dg variabel rasio akan membentuk
boxplot ternamai
state.x77$tingkatIncom<-cut(state.x77$Income, breaks=c(-Inf, 4000, 4800, Inf), labels=c("low",
"medium", "high"))
plot(state.x77$tingkatIncom, state.x77$Illiteracy)
#BUAT VARIABEL KATEGORIK
#fertility (0-36 low, 37-73 medium, 73< high) variabel baru namanya swiss$grfer
swiss$grfer<-cut(swiss$Fertility, breaks=c(0,36,73,Inf), labels=c("low", "medium","high"))
head(swiss)
plot(swiss$grfer,swiss$Fertility,xlab="Fertility Group",ylab="fertility", main="Fertility Boxplot by
Group of Fertility")
#education (0-2 lw, 3-10 medium, 10< high)
summary(swiss$Education)
swiss$gredu<-cut(swiss$Education, breaks=c(0,2,10,Inf), labels=c("low","medium","high"))
plot(swiss$gredu,swiss$Fertility, xlab="gred", ylab="fertility", main="fertility boxplot by group of
education")
#semakin tinggi tingkat pendidikan, semakin rendah fertilitasnya
#STEM N LEAF
stem(swiss$Fertility) #paling dominan di angka 60an dan 70an
#DIAGRAM QQ PLOT
qqnorm(swiss$Fertility)
qqline(swiss$Fertility)
#DIAGRAM BATANG
#jika data rasio langsung saja
barplot(state.x77$Income)
#jika data kategorik dibuat tabel dulu, atau data rasio bisa dibuat kategorik dulu biar rapi
tab<-table(state.x77$tingkatIncom)
barplot(tab, xlab="Income", ylab="frekuensi", main="Tingkat Income")
#tabel bisa dibuat didalam (feed adalah kategorik)
barplot(table(chickwts$feed), xlab="Feed", ylab="Frekuensi", col=c(2:6))
#diberi arsiran pada diagram
barplot(table(chickwts$feed), xlab="Feed", ylab="Frekuensi", density = c(10,20,30,40,50,60),
col=c(1:6))
#barplot menyamping tiap variabel
mat<-cbind(amer=wp$N.Amer,erup=wp$Europe,asia=wp$Asia)
barplot(mat, col=rainbow(7),beside = T, xlab="tahun", ylab="angka")
legend("topright", c("1951","1956","1957","1958","1959","1960","1961"), cex=0.7, pch=15, col =
rainbow(7))
#barplot tambahan
tab1<-table(swiss$Fertility)
tab1
barplot(tab1)
tab1_grfer<-table(swiss$grfer)
tab1_grfer
barplot(tab1_grfer)
barplot(tab2) #numpuk
barplot(tab2,beside=T) #menyamping
barplot(tab2,beside=T, col=c("red","yellow", "green"), xlab="grfer")
legend("topright",c("low","medium","high"),col=c("red","yellow", "green"),pch=1)
#GARIS
library(foreign)
dt<-read.spss("E:/Dhofirur/STIS/Tingkat 2/Asli/Komstat/Pertemuan 3/data_asli_1.sav",
to.data.frame = T)
head(dt)
plot(dt$No_Obs,dt$BI_Rate)
plot(dt$No_Obs,dt$BI_Rate, type="l", col="red", xlab="obsevasi", ylab="rate", ylim=c(0,15))
lines(dt$No_Obs,dt$Working_capital_rate,col="blue")
legend("bottomright", c("bi rate","working capital credit"), col=c("red","blue"),pch=5, cex=0.5)
#PELUANG NORMAL
#DIKETAHUI DARI 400 MAHASISWA STIS BERDISTRIBUDI NORMAL
#rata-rata tinggi badan adalah 165 cm dengan st.dev 5 cm
#berapa peluang seorang mahasiswa punya tinggi badan 164? P(X=164)
dnorm(164,mean=165,sd=5)
#BERAPA PELUANG SEORANG MAHASISWA PUNYA TINGGI BADAN KURANG DARI 164? P(X<164)
pnorm(164,mean=165,sd=5)
#P(X>164)
1-pnorm(164,mean=165,sd=5)
#JIKA DIKETAHUI PELUANGNYA ADALAH 0.05 BERAPA TINGGI BADAN MAKSIMAL MAHASISWA
TERSEBUT?
qnorm(0.05,mean=165,sd=5)
#NORMAL STANDAR
#P(X>1.96)
1-pnorm(1.96)
#MEMANGGIL
#CARA MANGGIL DATA DARI AXCEL
data<-read.csv("D:/data.csv")
data<-read.table("E:/Dhofirur/STIS/Tingkat 2/Asli/Komstat/data.txt,header=TRUE)
#EKSPLORASI DATA
#PLOT
plot(mtcars) #semuanya
plot(mtcars$hp,mtcars$mpg) #variabel tertentu
#KORELASI
cor(mtcars) #semuanya
library(corrplot) #packages untuk korelasi
corrplot(cor(mtcars),method = "ellipse") #korelasi dengan elips
#UJI ASUMSI
#NORMALITAS
#NOAUTOKORELASI
#HOMOSKEDASTISITAS
#NONMULTIKOLINEARITAS= UJI ASUMSI BAHWA VARIABLE INDEPENDENNYA TIDAK BERKORELASI
vif(rlb) #jika vif<5 maka asumsi nonmultikol terpenuhi
#PEMILIHAN MODEL
#BACKWARD= PEMILIHAN MODEL TERBAIK DENGAN MEMASUKAN SEMUA VARIABEL X LALU
DIKELUARKAN 1 PER SATU X
rlb_backward<-step(rlb,direction = "backward")
summary(rlb_backward)
#FORWARD= PEMILIHAN MODEL TERBAIK DENGAN MEMASUKAN X 1 PER 1
rlb_intersep<-lm(state.x77$Illiteracy~1) #sebagai intersep
summary(rlb_intersep)
rlb_forward<-step(rlb_intersep,scope=list(lower=rlb_intersep,upper=rlb),direction="forward")
summary(rlb_forward)
#STEPWISE= KOMBINASI ANTARA BACWARD DAN FORWARD
rlb_stepwise<-step(rlb,direction = "both")
summary(rlb_stepwise)