###################### # Importing Data ########################## getwd() list.files() setwd("C:/Users/Sunthud/Desktop/") setwd("C:/Users/Sunthud/Dropbox/Teaching/RINTRO") #tab-delimited file: *.txt #Comma seperated value: *.csv data.csv <- read.table(file = "Ch2.csv", header = TRUE, sep = ",", na.strings = "-999") data.txt <- read.table(file = "Ch2.txt", header = TRUE, sep = "\t", na.strings = "-999") install.packages("foreign") library(foreign) data.spss <- read.spss(file = "Ch2.sav", to.data.frame = TRUE) # read.csv : sep = "," # read.csv2 : sep = "," data.csv2 <- read.table(file = "Ch2_1.csv", header = TRUE, sep = ",", na.strings = "-999") ### Exporting Data write.table(attitude, file = "ch2att.csv", sep = ",", col.names = TRUE, row.names = FALSE, na = "-999") write.table(attitude, file = "ch2att.txt", sep = "\t", col.names = TRUE, row.names = FALSE, na = "-999") obj <- lm(rating ~ complaints + privileges, data = attitude) save(obj, file = "regression.Rdata") rm(obj) load(file = "regression.Rdata") ### Exploring Data hist(airquality$Temp) hist(airquality$Temp, breaks = 15) hist(airquality$Temp, breaks = 15, xlab = "Temperature", ylab = "Frequency", main = "Air Temperature in New York", col = "red") qqnorm(airquality$Temp) qqline(airquality$Temp) boxplot(airquality$Temp) boxplot(airquality$Temp ~ airquality$Month, ylab = "Temperature in F", xlab = "Month") plot(airquality$Ozone, airquality$Wind) obj <- lm(airquality$Wind ~ airquality$Ozone) abline(obj) ###################### # Packages ########################## install.packages("coefplot") require(coefplot) coefplot(fit) install.packages("arm") arm::coefplot(fit) coefplot::coefplot(fit) sem::sem lavaan::sem ### Factor g <- rep(c("toyota", "honda", "mazda", "ford"), each = 5) y <- c(9, 8, 6, 7, 5, 8, 9, 6, 5, 4, 8, 6, 4, 5, 5, 5, 6, 7, 4, 3) summary(lm(y ~ g)) g <- rep(1:4, each = 5) g <- factor(g, labels = c("toyota", "honda", "mazda", "ford")) g <- rep(1:4, each = 5) y <- c(9, 8, 6, 7, 5, 8, 9, 6, 5, 4, 8, 6, 4, 5, 5, 5, 6, 7, 4, 3) summary(lm(y ~ g)) g <- as.factor(g) summary(lm(y ~ g)) g <- factor(rep(4:1, each = 5), labels = c("toyota", "honda", "mazda", "ford")) y <- c(9, 8, 6, 7, 5, 8, 9, 6, 5, 4, 8, 6, 4, 5, 5, 5, 6, 7, 4, 3) summary(lm(y ~ g)) g <- relevel(g, ref = "ford") summary(lm(y ~ g)) as.numeric(g) ses <- c(3, 1, 2, 3, 1, 2, 3, 1, 2, 1, 1, 3) ses <- factor(ses, labels = c("low", "medium", "high"), ordered = TRUE) att <- c(52, 45, 44, 43, 56, 57, 45, 44, 50, 43, 55, 46) summary(lm(att ~ ses)) ### list obj <- attitude$rating obj2 <- lm(rating ~ complaints, data = attitude) newlist <- list(obj, obj2) list(1, 2, 3) list(c(1, 2, 3)) aa <- list(a1 = "a", a2 = c(1, 2, 3), a3 = "new") names(aa) obj2 <- lm(rating ~ complaints, data = attitude) obj2[[1]] obj2[[2]] aa[[1]] aa[[3]] obj2[["coefficients"]] obj2$coefficients attitude[[1]] length(obj2) aa[[1]] <- "b"