-
Notifications
You must be signed in to change notification settings - Fork 65
/
Intro to R - Ch 09 - Alternate.R
162 lines (107 loc) · 4.04 KB
/
Intro to R - Ch 09 - Alternate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
############################################################
# Alternate R script to accompany Intro to R for Business, #
# Chapter 09, written by Troy Adair #
############################################################
# Clear out Console and Enviroment
rm(list=ls(all=TRUE))
cat("\014")
# Load the tidyverse
library(tidyverse)
# Load the previously referenced data frame in "YT_Sample_Validated.RData"
Scorecard <- read_csv("most-recent-cohorts-all-data-elements-1.zip")
str(Scorecard)
attach(Scorecard)
# Statistics on 1 numerical variable
summary(HIGHDEG)
mean(HIGHDEG)
median(HIGHDEG)
max(HIGHDEG)
min(HIGHDEG)
sum(HIGHDEG)
sd(HIGHDEG)
var(HIGHDEG)
# Statistics on 1 categorical variable
table(HIGHDEG)
n <- length(Scorecard$HIGHDEG)
Scorecard$HDEGREE <- ""
for(i in 1:n) {
if(Scorecard$HIGHDEG[i]==0L) {
Scorecard$HDEGREE[i] <- "0 - Non-Degree"
} else if(Scorecard$HIGHDEG[i]==1L) {
Scorecard$HDEGREE[i] <- "1 - Certificate"
} else if(Scorecard$HIGHDEG[i]==2L) {
Scorecard$HDEGREE[i] <- "2 - Associate's"
} else if(Scorecard$HIGHDEG[i]==3L) {
Scorecard$HDEGREE[i] <- "3 - Bachelor's"
} else if(Scorecard$HIGHDEG[i]==4L) {
Scorecard$HDEGREE[i] <- "4 - Graduate"
} else{}
}
head(HDEGREE,10)
attach(Scorecard)
head(HDEGREE,10)
table(HDEGREE)
# Statistics on 2 categorical variables
table(HDEGREE, CONTROL)
Scorecard$ITYPE <- ""
for(i in 1:n) {
if(CONTROL[i]==1L) {
Scorecard$ITYPE[i] <- "Public"
} else if(CONTROL[i]==2L) {
Scorecard$ITYPE[i] <- "Private Non-Profit"
} else if(CONTROL[i]==3L) {
Scorecard$ITYPE[i] <- "Private For-Profit"
} else{}
}
attach(Scorecard)
table(HDEGREE,ITYPE)
# Statistics on 1 categorical and 1 numerical variable
by(ADM_RATE,ITYPE,summary)
str(Scorecard$ADM_RATE)
Scorecard$ADM_RATE <- as.numeric(Scorecard$ADM_RATE)
str(Scorecard$ADM_RATE)
print(str(Scorecard$ADM_RATE,digits=4))
by(ADM_RATE,ITYPE,summary)
by(Scorecard$ADM_RATE,ITYPE,summary)
attach(Scorecard)
by(ADM_RATE,ITYPE,summary)
by(ADM_RATE,ITYPE,mean)
by(ADM_RATE,ITYPE,mean,na.rm=TRUE)
by(ADM_RATE,ITYPE,sd,na.rm=TRUE)
# Statistics for 2 numerical values (i.e., simple linear regression, AKA "OLS")
cor(PCIP27,SATMTMID)
str(PCIP27)
str(SATMTMID)
Scorecard$PCIP27 <- as.numeric(Scorecard$PCIP27)
Scorecard$SATMTMID <- as.numeric(Scorecard$SATMTMID)
attach(Scorecard)
cor(PCIP27,SATMTMID)
cor(PCIP27,SATMTMID,use="complete.obs")
cov(PCIP27,SATMTMID,use="complete.obs")
OLS <- lm(SATMTMID~PCIP27)
OLS
summary(OLS)
# Could we have kept ourselves from having to convert so many character objects?
str(Scorecard)
cls <- c(LATITUDE="numeric",LONGITUDE="numeric", ADM_RATE="numeric",
ADM_RATE_ALL="numeric",SATVR25="numeric",SATVR75="numeric",SATMT25="numeric",
SATMT75="numeric",SATWR25="numeric",SATWR75="numeric",SATVRMID="numeric",
SATMTMID="numeric",SATWRMID="numeric",ACTCM25="numeric",ACTCM75="numeric",
ACTEN25="numeric",ACTEN75="numeric",ACTMT25="numeric",ACTMT75="numeric",
ACTWR25="numeric",ACTWR75="numeric",ACTCMMID="numeric",ACTENMID="numeric",
ACTMTMID="numeric",ACTWRMID="numeric",SAT_AVG="numeric",SAT_AVG_ALL="numeric",
PCIP01="numeric",PCIP03="numeric",PCIP04="numeric",PCIP05="numeric",
PCIP09="numeric",PCIP10="numeric",PCIP11="numeric",PCIP12="numeric",
PCIP13="numeric",PCIP14="numeric",PCIP15="numeric",PCIP16="numeric",
PCIP19="numeric",PCIP22="numeric",PCIP23="numeric",PCIP24="numeric",
PCIP25="numeric",PCIP26="numeric",PCIP27="numeric",PCIP29="numeric",
PCIP30="numeric",PCIP31="numeric",PCIP38="numeric",PCIP39="numeric",
PCIP40="numeric",PCIP41="numeric",PCIP42="numeric",PCIP43="numeric",
PCIP44="numeric",PCIP45="numeric",PCIP46="numeric",PCIP47="numeric",
PCIP48="numeric",PCIP49="numeric",PCIP50="numeric",PCIP51="numeric",
PCIP52="numeric",PCIP54="numeric")
head(cls)
str(cls)
unzip("most-recent-cohorts-all-data-elements-1.zip")
temp <- read.csv("most-recent-cohorts-all-data-elements-1.csv", colClasses=cls,na.strings='NULL')
str(temp)