R examples

fio <- read.csv(file="C:\\Users\\Kyle\\Desktop\\a_Delphix\\fio\\fio.csv",head=TRUE,sep=",")

library(scatterplot3d)
attach(fio)
s3d <-scatterplot3d(users, MB,ms,pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot")

s3d <-scatterplot3d(users, MB,ms,pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot", x.ticklabs=c(1,8,16,24,32,40,48,56,64) , xlim=c(0,60) )

s3d <-scatterplot3d(users, MB,ms,pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot",  xlim=c(1,64) )

s3d <-scatterplot3d(users, MB,ms,pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot", lab=c(3,4 ), lab.z=c(4) ,  xlim=c(1,64)  )

fit <- lm(mpg ~ wt+disp)
s3d$plane3d(fit)

scatter3d(users, MB,ms)



## Script for Part 1: Orientation     ##
##    John Fox                        ##
## An Introduction to R               ##
##    UCLA Feb. 2005                  ##
##------------------------------------##

    # Basics

# arithmetic, interacting with the interpreter

    # basic arithmetic operations

2+3
2-3
2*3
2/3
2^3


    # precedence of operators
        
4^2-3*2
(4^2) - (3*2) # use parentheses and spaces to group, clarify

-2--3
-2 - -3

1 - 6 + 4

2^-3


# functions, arguments to functions, obtaining help

log(100)
log(100, base=10)
log(100, b=10)

help(log)
?log
apropos("log")
help.search("log")

log(100,10)

"+"(2,3)

# vectors

c(1,2,3,4)  # combine

1:4     # sequence operator
4:1
-1:2    # note precedence
seq(1,4)
seq(2, 8, by=2)
seq(0, 1, by=.1)
seq(0, 1, length=11)

    # vectorized arithmetic
    
c(1,2,3,4)/2    
c(1,2,3,4)/c(4,3,2,1)
log(c(0.1,1,10,100), 10)

c(1,2,3,4) + c(4,3)
c(1,2,3,4) + c(4,3,2)

# variables

x <- c(1,2,3,4)
x
x/2
y <- sqrt(x)
y
x <- rnorm(100)
x
summary(x)  # a "generic" function

    # basic indexing

x[21]  
x[11:20]
x[-(11:100)]    # careful here!

z <- x[1:10]
z
z < -0.5
z > 0.5
z < -0.5 | z > 0.5   # | is vectored "or", & is "and"
abs(z) > 0.5
z[abs(z) > 0.5] # indexing by a logical vector


# user-defined functions

mean(x)
sum(x)/length(x)

my.mean <- function(x) sum(x)/length(x)
my.mean(x)
my.mean(y)
my.mean(1:100)
x

    # Duncan example
    
# creating a data frame from data stored in a file

Duncan <- read.table('http://socserv.socsci.mcmaster.ca/jfox/Courses/UCLA/Duncan.txt', header=TRUE)

Duncan
summary(Duncan)

# attaching a data frame

prestige

attach(Duncan)
prestige

# distributions and bivariate relationships

hist(prestige)

plot(income, education)
noteworthy <- identify(income, education, row.names(Duncan))
noteworthy
row.names(Duncan)[noteworthy]

pairs(cbind(prestige,income,education),
    panel=function(x,y){
        points(x,y)
        abline(lm(y~x), lty=2)
        lines(lowess(x,y))
        },
    diag.panel=function(x){
        par(new=TRUE)
        hist(x, main="", axes=FALSE)
        }
    )

# fitting a regression

duncan.model <- lm(prestige ~ income + education)
duncan.model
summary(duncan.model)

# regression diagnostics

library(car)

hist(rstudent(duncan.model))
qq.plot(duncan.model, labels=row.names(Duncan), simulate=TRUE)
    
graphs
http://www.statmethods.net/graphs/scatterplot.html

csv files
http://www.cyclismo.org/tutorial/R/input.html#read


http://socserv.mcmaster.ca/jfox/Courses/UCLA/Part-1-script.R

plot(hatvalues(duncan.model))
abline(h = c(2,3)*3/45)
identify(1:45, hatvalues(duncan.model), row.names(Duncan))

plot(cookd(duncan.model))
abline(h = 4/(45-2-1))
identify(1:45, cookd(duncan.model), row.names(Duncan))

av.plots(duncan.model, labels=row.names(Duncan))
cr.plots(duncan.model)

# refitting the model

which.names(c('minister', 'conductor'), Duncan)
duncan.model.2 <- update(duncan.model, subset=-c(6, 16))
summary(duncan.model.2)



Comments