Skip to content

Instantly share code, notes, and snippets.

@naiquevin
Last active August 29, 2015 14:02
Show Gist options
  • Save naiquevin/9ee5386b9eda60b2dc0e to your computer and use it in GitHub Desktop.
Save naiquevin/9ee5386b9eda60b2dc0e to your computer and use it in GitHub Desktop.
Hands on R session from Machine learning workshop
### Hands on R session by Harshad Saykhedkar from today's Machine
### learning workshop (Fifth elephant Mumbai run-up event)
---
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: i686-pc-linux-gnu (32-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> options(STERM='iESS', str.dendrogram.last="'", editor='emacsclient', show.error.locations=TRUE)
> require('tm')
Loading required package: tm
> typeof(1)
[1] "double"
> typeof("c")
[1] "character"
> typeof("car")
[1] "character"
> typeof(TRUE)
[1] "logical"
> typeof(FALSE)
[1] "logical"
> as.factor(c("a", "b"))
[1] a b
Levels: a b
> c("a", "b")
[1] "a" "b"
> typeof(c("a", "b"))
[1] "character"
> is.vector(c("a", "b"))
[1] TRUE
> class(c("a", "b"))
[1] "character"
>
> list(c("a", "b"), 1)
[[1]]
[1] "a" "b"
[[2]]
[1] 1
> library(datasets)
> library(help = "datasets")
Information on package ‘datasets’
Description:
Package: datasets
Version: 3.1.0
Priority: base
Title: The R Datasets Package
Author: R Core Team and contributors worldwide
Maintainer: R Core Team <R-core@r-project.org>
Description: Base R datasets
License: Part of R 3.1.0
Built: R 3.1.0; ; 2014-04-10 20:21:40 UTC; unix
Index:
AirPassengers Monthly Airline Passenger Numbers 1949-1960
BJsales Sales Data with Leading Indicator
BOD Biochemical Oxygen Demand
CO2 Carbon Dioxide Uptake in Grass Plants
ChickWeight Weight versus age of chicks on different diets
DNase Elisa assay of DNase
EuStockMarkets Daily Closing Prices of Major European Stock
Indices, 1991-1998
Formaldehyde Determination of Formaldehyde
HairEyeColor Hair and Eye Color of Statistics Students
Harman23.cor Harman Example 2.3
Harman74.cor Harman Example 7.4
Indometh Pharmacokinetics of Indomethacin
InsectSprays Effectiveness of Insect Sprays
JohnsonJohnson Quarterly Earnings per Johnson & Johnson Share
LakeHuron Level of Lake Huron 1875-1972
LifeCycleSavings Intercountry Life-Cycle Savings Data
Loblolly Growth of Loblolly pine trees
Nile Flow of the River Nile
Orange Growth of Orange Trees
OrchardSprays Potency of Orchard Sprays
PlantGrowth Results from an Experiment on Plant Growth
Puromycin Reaction Velocity of an Enzymatic Reaction
Theoph Pharmacokinetics of Theophylline
Titanic Survival of passengers on the Titanic
ToothGrowth The Effect of Vitamin C on Tooth Growth in
Guinea Pigs
UCBAdmissions Student Admissions at UC Berkeley
UKDriverDeaths Road Casualties in Great Britain 1969-84
UKLungDeaths Monthly Deaths from Lung Diseases in the UK
UKgas UK Quarterly Gas Consumption
USAccDeaths Accidental Deaths in the US 1973-1978
USArrests Violent Crime Rates by US State
USJudgeRatings Lawyers' Ratings of State Judges in the US
Superior Court
USPersonalExpenditure Personal Expenditure Data
VADeaths Death Rates in Virginia (1940)
WWWusage Internet Usage per Minute
WorldPhones The World's Telephones
ability.cov Ability and Intelligence Tests
airmiles Passenger Miles on Commercial US Airlines,
1937-1960
airquality New York Air Quality Measurements
anscombe Anscombe's Quartet of 'Identical' Simple Linear
Regressions
attenu The Joyner-Boore Attenuation Data
attitude The Chatterjee-Price Attitude Data
austres Quarterly Time Series of the Number of
Australian Residents
beavers Body Temperature Series of Two Beavers
cars Speed and Stopping Distances of Cars
chickwts Chicken Weights by Feed Type
co2 Mauna Loa Atmospheric CO2 Concentration
crimtab Student's 3000 Criminals Data
datasets-package The R Datasets Package
discoveries Yearly Numbers of Important Discoveries
esoph Smoking, Alcohol and (O)esophageal Cancer
euro Conversion Rates of Euro Currencies
eurodist Distances Between European Cities
faithful Old Faithful Geyser Data
freeny Freeny's Revenue Data
infert Infertility after Spontaneous and Induced
Abortion
iris Edgar Anderson's Iris Data
islands Areas of the World's Major Landmasses
lh Luteinizing Hormone in Blood Samples
longley Longley's Economic Regression Data
lynx Annual Canadian Lynx trappings 1821-1934
morley Michelson Speed of Light Data
mtcars Motor Trend Car Road Tests
nhtemp Average Yearly Temperatures in New Haven
nottem Average Monthly Temperatures at Nottingham,
1920-1939
npk Classical N, P, K Factorial Experiment
occupationalStatus Occupational Status of Fathers and their Sons
precip Annual Precipitation in US Cities
presidents Quarterly Approval Ratings of US Presidents
pressure Vapor Pressure of Mercury as a Function of
Temperature
quakes Locations of Earthquakes off Fiji
randu Random Numbers from Congruential Generator
RANDU
rivers Lengths of Major North American Rivers
rock Measurements on Petroleum Rock Samples
sleep Student's Sleep Data
stackloss Brownlee's Stack Loss Plant Data
state US State Facts and Figures
sunspot.month Monthly Sunspot Data, from 1749 to "Present"
sunspot.year Yearly Sunspot Data, 1700-1988
sunspots Monthly Sunspot Numbers, 1749-1983
swiss Swiss Fertility and Socioeconomic Indicators
(1888) Data
treering Yearly Treering Data, -6000-1979
trees Girth, Height and Volume for Black Cherry Trees
uspop Populations Recorded by the US Census
volcano Topographic Information on Auckland's Maunga
Whau Volcano
warpbreaks The Number of Breaks in Yarn during Weaving
women Average Heights and Weights for American Women
> ?cars
> head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
> print(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
7 10 18
8 10 26
9 10 34
10 11 17
11 11 28
12 12 14
13 12 20
14 12 24
15 12 28
16 13 26
17 13 34
18 13 34
19 13 46
20 14 26
21 14 36
22 14 60
23 14 80
24 15 20
25 15 26
26 15 54
27 16 32
28 16 40
29 17 32
30 17 40
31 17 50
32 18 42
33 18 56
34 18 76
35 18 84
36 19 36
37 19 46
38 19 68
39 20 32
40 20 48
41 20 52
42 20 56
43 20 64
44 22 66
45 23 54
46 24 70
47 24 92
48 24 93
49 24 120
50 25 85
> head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
> cars[1, ]
speed dist
1 4 2
> seq(1, 10)
[1] 1 2 3 4 5 6 7 8 9 10
> cars[seq(1, 10), ]
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
7 10 18
8 10 26
9 10 34
10 11 17
> names(cars)
[1] "speed" "dist"
> cars$speed
[1] 4 4 7 7 8 9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15
[26] 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24 24 24 24 25
> cars$dist
[1] 2 10 4 22 16 10 18 26 34 17 28 14 20 24 28 26 34 34 46
[20] 26 36 60 80 20 26 54 32 40 32 40 50 42 56 76 84 36 46 68
[39] 32 48 52 56 64 66 54 70 92 93 120 85
> cars[["speed"]]
[1] 4 4 7 7 8 9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15
[26] 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24 24 24 24 25
> typeof(cars$dist)
[1] "double"
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> mtcars[, c("mpg", "cyl", "wt")]
mpg cyl wt
Mazda RX4 21.0 6 2.620
Mazda RX4 Wag 21.0 6 2.875
Datsun 710 22.8 4 2.320
Hornet 4 Drive 21.4 6 3.215
Hornet Sportabout 18.7 8 3.440
Valiant 18.1 6 3.460
Duster 360 14.3 8 3.570
Merc 240D 24.4 4 3.190
Merc 230 22.8 4 3.150
Merc 280 19.2 6 3.440
Merc 280C 17.8 6 3.440
Merc 450SE 16.4 8 4.070
Merc 450SL 17.3 8 3.730
Merc 450SLC 15.2 8 3.780
Cadillac Fleetwood 10.4 8 5.250
Lincoln Continental 10.4 8 5.424
Chrysler Imperial 14.7 8 5.345
Fiat 128 32.4 4 2.200
Honda Civic 30.4 4 1.615
Toyota Corolla 33.9 4 1.835
Toyota Corona 21.5 4 2.465
Dodge Challenger 15.5 8 3.520
AMC Javelin 15.2 8 3.435
Camaro Z28 13.3 8 3.840
Pontiac Firebird 19.2 8 3.845
Fiat X1-9 27.3 4 1.935
Porsche 914-2 26.0 4 2.140
Lotus Europa 30.4 4 1.513
Ford Pantera L 15.8 8 3.170
Ferrari Dino 19.7 6 2.770
Maserati Bora 15.0 8 3.570
Volvo 142E 21.4 4 2.780
> mtcars[1:5, c("mpg", "cyl", "wt")]
mpg cyl wt
Mazda RX4 21.0 6 2.620
Mazda RX4 Wag 21.0 6 2.875
Datsun 710 22.8 4 2.320
Hornet 4 Drive 21.4 6 3.215
Hornet Sportabout 18.7 8 3.440
> mtcars[1:5, c(1, 2, 3)]
mpg cyl disp
Mazda RX4 21.0 6 160
Mazda RX4 Wag 21.0 6 160
Datsun 710 22.8 4 108
Hornet 4 Drive 21.4 6 258
Hornet Sportabout 18.7 8 360
> mtcars[1:5, -c(1, 2)]
disp hp drat wt qsec vs am gear carb
Mazda RX4 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 360 175 3.15 3.440 17.02 0 0 3 2
> dim(mtcars)
[1] 32 11
> rownames(mtcars)
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D" "Merc 230"
[10] "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
[22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
[28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
> names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
> plot(mtcars$disp)
> plot(mtcars$cyl, mtcars$mpg)
> ?pairs
> pairs(cyl ~ mpg + hp, data=mtcars)
> mtcars$mpg
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
> mtcars$mpg > 10
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[31] TRUE TRUE
> mtcars[mtcars$mpg > 10]
Error in `[.data.frame`(mtcars, mtcars$mpg > 10) :
undefined columns selected
> mtcars$mpg > 20
[1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
[25] FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE
> mtcars[mtcars$mpg > 20,]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
> sqrt(mtcars$mpg)
[1] 4.582576 4.582576 4.774935 4.626013 4.324350 4.254409 3.781534 4.939636
[9] 4.774935 4.381780 4.219005 4.049691 4.159327 3.898718 3.224903 3.224903
[17] 3.834058 5.692100 5.513620 5.822371 4.636809 3.937004 3.898718 3.646917
[25] 4.381780 5.224940 5.099020 5.513620 3.974921 4.438468 3.872983 4.626013
> mtcars[sqrt(mtcars$mpg) < 5,]
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
> mtcars$newvar <- sqrt(mtcars$mpg)
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb newvar
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 4.582576
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 4.582576
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4.774935
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 4.626013
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 4.324350
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 4.254409
> mean(mtcars$mpg)
[1] 20.09062
> summary(mtcars$mpg)
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.42 19.20 20.09 22.80 33.90
> quantile(mtcars$mpg, seq(0, 1, 0.1))
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
10.40 14.34 15.20 15.98 17.92 19.20 21.00 21.47 24.08 30.09 33.90
> sapply(mtcars, summary)
mpg cyl disp hp drat wt qsec vs am gear carb
Min. 10.40 4.000 71.1 52.0 2.760 1.513 14.50 0.0000 0.0000 3.000 1.000
1st Qu. 15.42 4.000 120.8 96.5 3.080 2.581 16.89 0.0000 0.0000 3.000 2.000
Median 19.20 6.000 196.3 123.0 3.695 3.325 17.71 0.0000 0.0000 4.000 2.000
Mean 20.09 6.188 230.7 146.7 3.597 3.217 17.85 0.4375 0.4062 3.688 2.812
3rd Qu. 22.80 8.000 326.0 180.0 3.920 3.610 18.90 1.0000 1.0000 4.000 4.000
Max. 33.90 8.000 472.0 335.0 4.930 5.424 22.90 1.0000 1.0000 5.000 8.000
newvar
Min. 3.225
1st Qu. 3.927
Median 4.382
Mean 4.435
3rd Qu. 4.775
Max. 5.822
> hist(mtcars$mpg)
> density(mtcars$mpg)
Call:
density.default(x = mtcars$mpg)
Data: mtcars$mpg (32 obs.); Bandwidth 'bw' = 2.477
x y
Min. : 2.97 Min. :6.481e-05
1st Qu.:12.56 1st Qu.:5.461e-03
Median :22.15 Median :1.926e-02
Mean :22.15 Mean :2.604e-02
3rd Qu.:31.74 3rd Qu.:4.530e-02
Max. :41.33 Max. :6.795e-02
> plot(density(mtcars$mpg))
> quantile(mtcars, seq(0.9, 1, 0.005))
Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) :
undefined columns selected
> quantile(mtcars$mpg, seq(0.9, 1, 0.005))
90% 90.5% 91% 91.5% 92% 92.5% 93% 93.5% 94% 94.5%
30.0900 30.4000 30.4000 30.4000 30.4000 30.4000 30.4000 30.4000 30.6800 30.9900
95% 95.5% 96% 96.5% 97% 97.5% 98% 98.5% 99% 99.5%
31.3000 31.6100 31.9200 32.2300 32.5050 32.7375 32.9700 33.2025 33.4350 33.6675
100%
33.9000
> ?cars
> linregres <- lm(dist ~ speed, data = cars)
> linregres
Call:
lm(formula = dist ~ speed, data = cars)
Coefficients:
(Intercept) speed
-17.579 3.932
>
[1] "linrre"
> summary(linregres)
Call:
lm(formula = dist ~ speed, data = cars)
Residuals:
Min 1Q Median 3Q Max
-29.069 -9.525 -2.272 9.215 43.201
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.5791 6.7584 -2.601 0.0123 *
speed 3.9324 0.4155 9.464 1.49e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
> linregres <- lm(dist ~ 0 + speed, data = cars)
> summary(linregres)
Call:
lm(formula = dist ~ 0 + speed, data = cars)
Residuals:
Min 1Q Median 3Q Max
-26.183 -12.637 -5.455 4.590 50.181
Coefficients:
Estimate Std. Error t value Pr(>|t|)
speed 2.9091 0.1414 20.58 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 16.26 on 49 degrees of freedom
Multiple R-squared: 0.8963, Adjusted R-squared: 0.8942
F-statistic: 423.5 on 1 and 49 DF, p-value: < 2.2e-16
> plot(linregres)
Hit <Return> to see next plot:
Error in plot.new() : attempt to plot on null device
> plot(linregres)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
> predict(linregres)
1 2 3 4 5 6 7 8
11.63653 11.63653 20.36393 20.36393 23.27306 26.18219 29.09132 29.09132
9 10 11 12 13 14 15 16
29.09132 32.00045 32.00045 34.90959 34.90959 34.90959 34.90959 37.81872
17 18 19 20 21 22 23 24
37.81872 37.81872 37.81872 40.72785 40.72785 40.72785 40.72785 43.63698
25 26 27 28 29 30 31 32
43.63698 43.63698 46.54611 46.54611 49.45525 49.45525 49.45525 52.36438
33 34 35 36 37 38 39 40
52.36438 52.36438 52.36438 55.27351 55.27351 55.27351 58.18264 58.18264
41 42 43 44 45 46 47 48
58.18264 58.18264 58.18264 64.00091 66.91004 69.81917 69.81917 69.81917
49 50
69.81917 72.72830
> predict(linregres, data.frame(speed = c(0,3)))
1 2
0.000000 8.727396
> ?eurodist
> # hierarchical clustering
> temp <- as.matrix(eurodist)
> rownames(temp)
[1] "Athens" "Barcelona" "Brussels" "Calais"
[5] "Cherbourg" "Cologne" "Copenhagen" "Geneva"
[9] "Gibraltar" "Hamburg" "Hook of Holland" "Lisbon"
[13] "Lyons" "Madrid" "Marseilles" "Milan"
[17] "Munich" "Paris" "Rome" "Stockholm"
[21] "Vienna"
> dim(temp)
[1] 21 21
> head(temp)
Athens Barcelona Brussels Calais Cherbourg Cologne Copenhagen Geneva
Athens 0 3313 2963 3175 3339 2762 3276 2610
Barcelona 3313 0 1318 1326 1294 1498 2218 803
Brussels 2963 1318 0 204 583 206 966 677
Calais 3175 1326 204 0 460 409 1136 747
Cherbourg 3339 1294 583 460 0 785 1545 853
Cologne 2762 1498 206 409 785 0 760 1662
Gibraltar Hamburg Hook of Holland Lisbon Lyons Madrid Marseilles
Athens 4485 2977 3030 4532 2753 3949 2865
Barcelona 1172 2018 1490 1305 645 636 521
Brussels 2256 597 172 2084 690 1558 1011
Calais 2224 714 330 2052 739 1550 1059
Cherbourg 2047 1115 731 1827 789 1347 1101
Cologne 2436 460 269 2290 714 1764 1035
Milan Munich Paris Rome Stockholm Vienna
Athens 2282 2179 3000 817 3927 1991
Barcelona 1014 1365 1033 1460 2868 1802
Brussels 925 747 285 1511 1616 1175
Calais 1077 977 280 1662 1786 1381
Cherbourg 1209 1160 340 1794 2196 1588
Cologne 911 583 465 1497 1403 937
> ?hclust
> hclustres <- hclust(eurodist)
> plot(hclustres)
> head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment