forked from tra418/CSB-311-Spring-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntro to R - Ch 09 - Alternate.R
133 lines (81 loc) · 2.44 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
############################################################
# Alternate R script to accompany Intro to R for Business, #
# Chapter 09, written by Troy Adair #
############################################################
# First, clear memory and the Console
rm(list=ls(all=TRUE))
cat("\014")
library(here)
library(tidyverse)
# Load the previously referenced data frame in "YT_Sample_Validated.RData"
Scorecard <- read_csv(here("Data","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)
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)
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)