Statistics 6D,  Computing Tips Page
 
 


Parts:

1.    Internet and Email Access

2.    General Remarks on Computing

3.    Web Page Development

4.    First Class Assignment:    Create your own Home Page

5.    Importing graphics to web pages:

6.    Second Class Assignment:    Add a graphic to your web page

7.    More Netscape Composer Tips

8.    Microsoft Excel & Simple Graphics

9.    Microsoft Excel & Histograms

10.    Microsoft Excel and Random Number Generation

11.    Introduction to Matlab

12.    More with Matlab

13.    Graphics in Matlab

14.    Importing Data into Matlab

15.    Matlab Script Files

16.    Kernel Density Estimation using Matlab
 
 


1. Internet and Email access (discused 8/20/02):

*  Access to this web page is essential for this course.  Many important things will be posted here.

*  Class ListServ.  This is an essential device for staying on top of course announcements (e.g. cancelled classes, changes made to the web page, etc.).

*    Pre-enrolled class members have been already subscribed to the list, and don't need to take further action (unless you want to unsubscribe).

*    If you are not subscribed, but want to join, go to the URL: http://listserv.unc.edu/cgi-bin/lyris.pl?enter=Stat6D

*    If you want to make some changes in your subscription (e.g. to unsubscribe), go to the URL: http://listserv.unc.edu/cgi-bin/lyris.pl?enter=Stat6D   (note that you initially have an "empty" password)
 
 


2. General Remarks on Computing (discused 8/20/02):

+  "A tool, not an end in itself".  Intended to "facilitate thought, not replace it".

+  But vital for "serious visualization"

+  Nearly never done in class (so don't bring your notebook computer).

+  Done with CCI Computers.

+  General rules for when you encounter a problem:

        1.    "Just try something"

        2.    Ask a friend, or somebody hanging around.

        3.    Seek "more official" help.
 
 


3. Web Page Development (discused 8/20/02):

    -    Can use your favorite method

    -    Suggest "Netscape Composer", accessed from any of:

                -    Icon on desktop

                -    Start Button    -->   Programs   -->   Netscape Communicator   -->
                                                      -->   Netscape Composer

                -    Right Clicking File, and choosing "Edit"   (most useful?)

    -    Natural to use, "What You See Is What You Get", environment

    -    Drawback:    limited in flexibility and scope

                -    thus can't do "serious" (commercial level) web development

                -    but quick and easy for simple things, which we will do

                -    more advanced approaches:  Dreamweaver, coding in .html
 
 


4. First Class Assignment (discused 8/20/02):    Create your own Home Page

    +    "Download" a sample page, e.g. from this link

    +    Suggested approach:

        a.    Create a new Folder (directory) on your machine, for Stat6d.

        b.    Click the "My Documents" Icon on the desktop    --->
                                --->   alt + file + new + Folder
                                --->   Type in Stat6D

        c.    Right Click on link (above)

        d.    Save link (target in Internet Explorer) as ....

        e.    Save to this new Stat6d folder
                                        ('My Documents' button is a useful shortcut)
               IMPORTANT: use the File name "index.html"
 

    +    Edit this page:

        a.    Right Click on the filename, and choose "Edit"
                                        (this starts up Netscape Composer)

        b.    Makes all changes that you feel like

        c.    Change the "Title" (appears on the top blue bar), by:
                      format    --->    Page Colors and Properties   --->    Title

        d.    During (and after) this process, be sure to "save your work":
                      file    --->    save
 

    +    Put this page into your HTML accessible space on Isis:

        a.    Use the "FTP" software WS_FTP95 LE
                             (either as icon on desktop, or from "Programs")

        b.    Set up (for later use) a "Session Profile", in the box that first appears

        c.    Use the Profile name "Isis", or "My Isis web space", or something

        d.    Host Name / Address:  isis.unc.edu

        e.    UserID:   {your ONYEN}

        f.     Password      (this must be your ONYEN password,
                                              and can be saved for next time)

        g.    Click "OK" to log on to Isis.

        h.    Change Directory on ISIS (Right hand window) to "public_html"

   (do this by clicking on icons representing folders, to go "down",
or click on the green arrow to go "up",
CAUTION:   do not use the "ChgDir" Button,
unless you understand the AFS file system)

        e.    Change Directory on your notebook (Left hand window) to file location

        f.    Transfer the file "Index.html" by pushing the arrow button

(between the windows)

        g.    Check the file transfer, by pointing your browser to

http://www.unc.edu/~{your onyen}/    (use lowercase)

        h.    CAUTION:  the file you downloaded will only be found
                        if it is named "index.html"

        i.    CAUTION:  Changes made on the version on your computer
                        will not appear in your browser, until:

                1.    The file is again transferred with FTP as above.

                2.    The "Refresh" button on the browser is used.

        j.    Next week, will learn about "AFS file transfers" (nicer than FTP)
 

    +    Email the link to me
 
 


5. Importing graphics to web pages (discused 8/22/02):

    -    Again, there are many ways to do this

    -    Here is one simple but useful one:

            1.    Highlight the window with what you want in it (click on it)

            2.    Push both the alt and PrtScrn buttons

(this "copies" an image version of the window)

            3.    Start up Microsoft Photo Editor (e.g. from Icon or Program Menu)

(if you don't have an icon, you might want to create one)

            4.    Use:    Edit    --->    Paste as new image

(this shows you the image that you saved in Step 3)

            5.    Now there are a number of things that you might want to fiddle:

                    a.    The view size (change the box with "__%")

                    b.    Subimage selection (push the button with the dashed box)

                    c.    "Crop" (cut off unwanted parts), after selecting as in (b)

                    d.    "Resize", changes actual image size (not just view as in (a))

                    e.    "Rotate"

                    f.    Many others, jus explore menus, and try

            6.    Use Ctrl+Insert to "copy" the latest version

            7.    Inside Netscape Composer, use Shift+Insert to "paste" the image

("Medium resolution" is usually right)

            8.    It can be useful to click on the image, to adjust various "properties"

(e.g. vertical alignment may be important)

            9.    As always don't forget to "Save" your changes.
 
 


6. Second Class Assignment (discused 8/22/02):

find a cool looking image on the web,

                and add it to your own web page, (perhaps) using these steps
 
 


7. More Netscape Composer Tips (discused 8/29/02):

    +   Control Background Color (and even add a background image) with:

Format    --->    Page Colors and Properties

    +   Control Text Color with:    "arrow next to (black) color block"

    +   Control Text "Justification" (start at left, centered, ...), "block indenting" with:

"text pics with arrow" buttons

    +   Control Text Font and Size with menu boxes (on left)

    +   Make text Bold, Italic or Underlined with:  A, A, A buttons keys

    +   Check spelling with "Spelling" button

    +   Create a link to another location in the same page:

            -    E.g. useful for "Table of Contents" (see top of this page)

            -    1st (in desired location) use the "Target" button, to flag location

            -    2nd (at jump point) create a "Link" (use "select a named target")

            -    Note can also point to targets in other pages

    +    Explore other features by trial and error

(just don't "save" after a major disaster)























8. Microsoft Excel & Simple Graphics (discused 9/5/02):

    *    General philosophy of Excel

            -    Excel is "spreadsheet software"

            -    Originally intending for accounting / record keeping

            -    But now very widely used for many different applications

            -    Format is "rectangular array" of data (numbers, text, ...)

            -    Locations are "indexed":     Letters for columns, Numbers for rows

            -    Locations are "linked":    i.e. table values can refer to each other

            -    "Active Recomputation" keeps all entries up to date when one changes

    *    Excel can be started in many ways

            -    Icon

            -    Start    --->    Programs    --->    Microsoft Excel

            -    Double Click on Excel file    (look for the "green X" symbol, or .xls)

    *    A Toy Example can be downloaded from here

            -    You choose the location (as for sample web page)

    *    Part 1 shows a "typical accounting example"

            -    Costs of items appear as numbers in cells C2, C3, C4

            -    Explanatory text appears in many other places

            -    Subtotal (of the three prices) appears cell C5

            -    Total uses "sum" function    (can get this from "" button)

            -    Experiment with "sum", and "interactive cell selection"

            -    Get Tax, using a "typed in formula", that refers to cell C5

            -    Get Total, using another typed in formula

            -    Change one of the prices

(note that everything is automatically updated)

            -    These properties make Excel a very powerful accounting tool

    *    Many "twiddles" are possible:

            -    Check out the "Toolbar" at the top

            -    E.g. can change fonts, sizes, justifications, colors, bold, ...

            -    Often useful to change cell sizes

(grab and move "boundaries" at top and left margins)

            -    Sometimes want to add rows or colums

(use    "Insert"   --->    ...)

            -    Sometimes want to delete rows or colums

(1st highlight in the margin,   then    Edit    --->    Delete)

    *    Can make "colored bar" between sections:

            -    by "colorizing the whole row"

            -    1st highlight the row

            -    Then use "fill color"
 

    *    Excel has some convenient graphics features

            -    Drag a single cell to replicate values

            -    Drag a pair of cells to get an "equally spaced grid"

            -    Drag a cell with a formula to replicate the formula

(note:  the index adapts correctly)

            -    Use the Chart Wizard (colored bar button) for graphics

            -    Can adjust many things during Wizard Process

            -    And also by clicking on graph parts later

            -    Replicate the graph in part 2 of the Toy Example?
 
 


9. Microsoft Excel & Histograms (discused 9/5/02):

    +    Excel has a useful "Histogram Tool"

    +    To make this available, you may first need to do:

Tools    --->    Add-ins    --->    Analysis Tool Pack

    +    An analysis of the "Buffalo Snowfall Data" is in Part 3 of the Toy Example

    +    The data are annual total snowfalls (inches) in Buffalo, N. Y.

    +    The raw data are in the cell range    B44:B106

    +    A simple calculation of bin counts is done using:

            -    Tools    --->    Data Analysis    --->    Histogram

            -    Input Range:    B44:B106

[can either type this into the window,  or click
the symbol at the right for "interactive selection"]

            -    Leave Bin Range empty, to allow to let Excell choose the bins

            -    Using an output range is suggested (to keep the answer on this "sheet")

            -    OK, gives tabel of "bins" and "frequencies" ( i.e. bin counts)

    +    As a "reality check", compute Sum of counts (calculated in cell F56)

    +    And compare to "number of data points" (calculated in cell F58)

    +    Check constant binwidth (calculated in cells G46:G52)

            -    Used "cell indices" in formula

            -    "Dragged one formula down" to created several

            -    Error message in cell G52  (because D53 is not a number)

    +    Adjust to make Total Area = 1:

            -    Use formula with "relative index" (E46), that changes when dragged)

            -    And also "absolute indices" (e.g. $F$58), that stay the same when dragged)

            -    Note only binwidth in F58 is needed (don't need to calculate others)

            -    Check Area = 1  (calculated in Cell I60)

    +    Next make Histogram Graphic (i.e. bar graph) using:

            -    Chart Wizard    --->    Column

            -    Series    -    Values:      cells I46-I53

            -    Categories X-axis labels:      cells D46-D53

            -    Fill out Chart Options as desired    (can also change these later)

            -    Some personal choices:

                    *    Gridlines:    uncheck "y - axis major gridlines"

                    *    Legend:    uncheck "show legend"

            -    Place Chart as Object in Sheet 1

            -    "Grab" chart and move to desired position

            -    Resize by grabbing little black boxes at edge

            -    Change other chart aspects by clicking on them

            -    Some personal favorites:

                    *    Plot Area:    Change area color to light blue

                    *    Bars:    Patterns    --->    color to Dark Blue

                    *    Bars:    Options    --->    Gapwidth = 0

    +    Next explore different binwidths:

            -    First create column of bin edges (see  D97:D133)

                    *    type 1st two, then "drag corner"

                    *    be sure to include "all of data" (smaller than max, larger than min)

            -    Use as "Bin Range" input in Histogram Data Analysis Tool

            -    Other steps as above
 
 


10. Microsoft Excel and Random Number Generation   (discused 9/10/02):

    -    Start with:    Tools    --->    Data Analysis    --->    Random Number Generation

    -    Fill in:

            *    Number of Variables:   (columns of spreadsheet)

            *    Number of Random Numbers:   (rows of spreadsheet)

            *    Distribution:    Uniform

(means all fractions between 0 and 1 are equally likely)

(some other choices are possible, we won't use)

            *    Output options:    prefer output range (then specifiy)

    -    Result is cells filled in with random numbers

    -    Note:  these are generated only once, and are never recomputed.

    -    For "recomputable" random numbers:

            *     Use the function (recall the  key) "rand"

            *    Note:  no "inputs", only an "output"

            *    Go to another cell, enter a calculation, and it changes!

            *    Basically does a "rethrow of the dice"
 


    -    Combine random numbers with spreadsheet operations to do many things

    -    For example, can use Excel to imitate gambling games

    -    E.g. Toss a Coin:

            *    Put "rand()" in a cell, say E7

            *    Use  "if" in another cell

            *    Use "E7 < 0.5" as the Logical_test

            *    Use "Head" as Value_if_true

            *    Use "Tail" as Value_if_false

            *    Can drag both cells to repeat experiment

    -    E.g.  Roll a die:

            *    Put "rand()" in a cell, say E7

            *    In another cell, use the "roundup" function

            *    For Number, use "6*E7"

            *    For Num_digits, use "0"

            *    Again will recompute when something is changed

            *    Again can drag for many replays
 
 


11. Introduction to Matlab (discussed 10/10/02)

Matlab is a very powerful numerical and graphical "computing environment"

But it uses "different modes of operations"

    I.e. different "computational philosophy"

    Instead of "point and click, windows environment"

    Matlab uses "command line environment"
 


Start Matlab by clicking on the icon

    -    Can choose "Current Directory", using window on top

    -    The "three dot button" is useful for browsing to find a good directory

    -    Most of action happens in "Command Window"

    -    Commands are typed after the "command prompt":    >>
 
 


Matlab can work like a very powerful calculator.

Try the following (hit "enter" after typing the rest):

>>    3+5

                (the answer appears as "ans = ...")

>>    16-10

>>    348975032975 * 987350987435

                (this is too big for most calculators)

                (note answer in "scientific notation")

>>    3^2

>>    3^3

>>    3^100

>>    1000^1000

                (some number are "too big")

>>    2 * (3 + 1)

                (can use parentheses as needed)

>>    1+2*3

                (Careful about "order of operations")

>>    2*3^2

                (Rule is:   1. Power    2. Mult. & Div.    3. Add. and Subt.)

>>    (1+2)*3

>>    1+(2*3)

>>    (2*3)^2

>>    2*(3^2)

                (Can always be sure to get it right using parentheses)

>>    1/0

                (note warning message)

>>    0/0

                (Warning message & returns value NaN = "Not a Number")

                (Why is this different from "Inf" above?)
 


Matlab uses scientific notation:

The part after the "e" is interpreted as "times 10 to the power"

>>    2e3

>>    1.234e1

>>    4e-2

>>    -4e2

>>    -4e-2
 


Matlab does "floating point arithmetic"

I.e. only keeps a certain number of significant digits
 

>>    0.123456789012345670 - 0.123456789012345671

                (not exactly right, because of rounding)

>>    0.1234567890123456780 - 0.1234567890123456781

                (Can't tell difference between these, so gives answer of "0")
 


Matlab has a very large number of functions available

Try these:

>>    sqrt(9)

>>    pi

>>    sin(pi/2)

>>    exp(1)

                (the "natural number", often denoted by "e")

>>    log10(1000)

>>    round(5.315)

>>    round(4.807)

>>    round(4.5)

>>    floor(5.315)

>>    floor(4.807)

>>    ceil(5.315)

>>    ceil(4.807)

                (heard of these last one before?)
 


A useful command is "help":

>>    help sqrt

>>    help log

>>    help

                (this shows a bewildering index of available functions)

>>    help elfun

                (Can see some of the "subtopics" under "help)

>>    help ops
 


Matlab allows "complex numbers":

>>    sqrt(-1)

>>    i*i

>>    (1+2i)*(2-3i)
 


12. More with Matlab (discussed 10/15/02)

In addition to standard calculator operations, Matlab is far more powerful:
 


Power Concept 1:   Assignments

Main Idea:    Give a "label" to a number (really a "space in computer memory")

then can use that in later operations, and "change it on the fly"

Try these:

>>    a = 3

the symbol "=" actually says "assign the value '3' to the 'a' label"

i.e. "in the 'a' location in memory, put the value '3'"

>>    a

>>    a+5

>>    a^2

>>    3^a

>>    a = 7

note that a has been reassigned now

>>    a

>>    a+3

>>    b = 5

>>    a * b

>>    (a + b) / 2

>>    a + b / 2

Recall "order of operations" issues  (division gets higher priority)

>>    (a - b - 2) / 0

>>    a = 1 ; b = 2; c = 3

Use ";" to put several statments on the same line

But only last one shows answer

>>    a + b; b * c; (a + b)^c

>>    a + b

>>    b * c

>>    (a + b)^c ;

Can avoid seeing answer by ";" at the end








>>    disp(a + b); disp(b * c); disp((a + b)^c)

But can force answer to appear using "disp"










Power Concept 2:   Loops

Main Idea:    Set of repeated operations

Try these:

>>    for i=1:10; disp(i) ; end ;

note:  the value of "i" changes through the loop

>>    for i=0:20; disp(2^i); end ;

>>    for i=0:20; disp(2^i);

note: it is waiting for the "end" statement, can type in later










Power Concept 3:   Vectors and matrices

Main Idea:    Store bigger "collections" of numbers

    "Vector":    a "row" or "column" of numbers

    "Matrix":    a "rectangular array" of numbers

Try these:

>>    v = [1 2 3]

>>    4+v

>>    3*v

>>    v+[4 5 6]

>>    v+[4 5]

Careful:    need to worry about "sizes"

>>    w = [10; 11; 12]

";" says "put down below"

>>    2*w+3

>>    u+v

Careful again about "size" issues

>>    v = 1 ;  for s = 2:15 ; v = [v; s] ; disp(v) ; end ;

>>    m1 = [[1 2]; [3 4]]

>>    m2 = [[m1 2*m1]; [4*m1 8*m1]]

>>    m2 = [[m1 2*m1]; [4*m1 8*m1]

>>    m2 = [m1 2*m1]; [4*m1 8*m1]]

need to be careful with brackets....

>>    for c = 2:10 ;  mv = [mv c*v] ; disp(mv) ; end
 
 


Some more examples:    Equally spaced grids, 0s, 1's, identity

>>    grid1 = 1:5

"give a range from 1 to 5"

>>    grid2 = (1:10)'

use the apostrophe for "matrix transpose"

(i.e. change rows to columns)

>>    grid1'

>>    [[1 2] ; [3 4]]'

>>    0:3:12

Putting a third number between specifies the "jump"

>>    0:-1:6

>>    linspace(1,5,5)

Here is another way to get an "equally spaced grid"

>>    linspace(1,5,10)

Format is "Start,  End,  Number of Points"

>>    zeros(5,3)

Sometimes a matrix of 0's is useful

>>    ones(6,4)

>>    eye(5)

This is the "identity" matrix (multiply by this to get the same thing)

>>    m1

>>    m1 * eye(2)

>>    m1 * eye(4)

The size of the matrix has to be right for this to work









Some more examples:    random number generation, means, etc.

>>    data = rand(2,3)

>>    data = rand(2,3)

Note:  a second "call to rand" gives a "new roll of the dice"

>>    sum(data)

>>    help sum

>>    sum(data,1)

>>    sum(data,2)

>>    mean(data)

>>    std(data)

>>    mean(data) - sum(data)/2

>>    help stats

>>    help boxplot
 
 


Interesting Aside:  the name "MatLab" comes from "Matrix Laboratory"

(not "Mathematics Laboratory"....)








13. Graphics in Matlab  (discussed 10/22/02)

Simple plots are very easy in Matlab:

>>  vx = 1:4

>>  vy = [3 0 5 2]

>>  plot(vx,vy)

(note:  the "default" is "connect the points with lines")

>>  plot(vx,vy,'*')

(not so easy to see, because they appear at the edges...)

(note:  have to click the icon to get pic back on top)

>>  plot(vx,vy,'o-')

(this gives both a "symbol" and a "line")

>>  plot(vx,vy,'rs--')

('r' is for "red",  's' is for "square",  '--' is for "dashed line")

>>  help plot

(gives a long list of options of this type, including colors, symbols and line types)

>>  my = [vy; 2*vy; 3*vy; 4*vy; 5*vy]

>>  plot(vx,my)

(gives "overlay" of several curves at once)






The  command "axis" provides some often useful control:
 

>>  plot(vx,vy,'*')

(here is a case where Matlab's chosen axes are pretty poor)

(much better would be:  min x = 0, max x = 5, min y = -1, max y = 6)

>>  axis([0 5 -1 6])

>>  help axis

(there are many other things that can be done with this command)

>>  axis([20 10 -1 -1])

(this does require intellingent choice of the values!)





>>  axis([10 20 -5 -1])

( be careful to choose a window that includes some data!)






Titles and Labels are easy to add

>>  title('First Try at Matlab Plotting')

(the quote marks determine a "string" i.e. set of text)

>>  title(First Try at Matlab Plotting)

(without the quotes, Matlab is looking for variable names)

>>  tempstr = 'A Revised Title'

(strings work like other Matlab variables)

>>  title(tempstr)

(can also feed a variable name into "title")

>>  xlabel('x') ;  ylabel('y')

(as before can have two commands, separated by a ";")






Many Plot Properties can be changed with the Graphics Properties Editor

Choose "Edit" from the Figure Window Menu Bar

Try "Axes Properties"

Can Reset "Scale",   "X" and "Y" to something more sensible

(try 0 5 -1 10,    note to high, so change last one to 6)

Can also fiddle many other things
 
 


Saving Plots

A simple way to save plots is:   ctrl+alt+PrtSc     (as before)

(Drawback:    only have screen resolution, rather coarse)

More powerful approach is using the "print" command:

    -    try:    >>  help print

(more info than you probably want)

    -    If you are connected to a printer, you can print out a paper copy

    -    For web design, ">>  print -dmeta"  or "print -dbitmap" are very useful

    -    They "save graphics to the buffer", which allows easy insertion into pages
 


An Example, to tie things together:

>>  vangle = linspace(0,2*pi,200)

>>  vx = cos(vangle)

>>  vy = sin(vangle)

>>  plot(vx,vy,'g-')

>>  title('Toy Circle')

>>  xlabel('x = cos(angle)') ;  ylabel('y = sin(angle)')

Use Graphics Property Editor to:

    -    Expand Scales to +-1.1

    -    Expand line width to 4

    -    Set "Aspect Ratio" to 1    (so looks like circle, not ellipse)

Use alt+PrtSc to save as graphics image

Paste into Microsoft Photo Editor

    -    Now can crop etc. as before

But a quicker approach is:

>>  print -dbitmap

And then directly paste into the web page
 
 


Box Plots

>>  help boxplot

(as usual many options and variations are possible)

>>  mdata = [randn(50,1) 3+randn(50,1) 2*randn(50,1) randn(50,1).^2]

>>  boxplot(mdata)

(note box plots appear as expected)

(but still have old titles and labels)

>>  close

(closes the graphics window, thus forgetting the previous settings)

Now rerun above:

>>  boxplot(mdata)

(as above can now fiddle many things)







14. Loading Data into Matlab  (discussed 10/24/02)
 

The simplest way to import data to Matlab is to type it in

>>  indat = [5 8 2 3 0]

    -    This is easy for very small data sets

    -    But way too time consuming for medium to large data sets

    -    General Rule:    never spend more than 2-3 minutes at this
 

A more realistic, but simple, approach is to import from text files

    -    Text files can be viewed (and edited) with "Microsoft Notepad" (for example)

    -    Get to notepad from    Start    --->    Programs    --->    Accessories

    -    For many text files, the Matlab command "load" will work
 

Try inputting the toy text file:     ToyInput1.txt

(first save this to the directory that is "current" for Matlab)

>>  help load

(as usual, there are many things that "load" can do,

but we care about the "load filename" near the beginning)

>>  load nothere.txt

(this gives an error message, because that file is not in the current directory)

>>  load ToyInput1.txt

(no response means the load was successful)

>>  who

(this command gives a list of all variables created so far)

(now there is a variable "ToyInput1", which we just loaded)

>>  ToyInput1

(this gives the 3 x 4 matrix matrix of numbers from the file)

>>  inmatrix = load('ToyInput1.txt')

(here is a way to save it as a variable with a different name from the input file)

>>  inmatrix = load(ToyInput1.txt)

(the quotes are important, because a "string" input is needed here)




The "load" command is somewhat limited, e.g. need a "matrix of numbers"

View and download this file: ToyInput2.txt

>>  inmatrix2 = load('ToyInput2.txt')

(this doesn't work, because the number of values differs for different lines)

(matlab really requires a matrix)

Some workarounds:

    -    Edit the file with notepad, and "pad with 0's"  (i.e. add 0's as needed)

    -    Use more advanced file loading, (see  >> help iofun)

(not recommended, unless have experience, since very time consuming)

    -    Import as Excel spreadsheet
 

Importing Excel Spreadsheets into Matlab

>>  help xlsread

    -    This shows a lot of info, as always

    -    Note Matlab pays careful attention to "numbers" and "text"

    -    Try it out on this Excel spreadsheet:    ToyInput2.xls

    -    Same as above, but with text labelling Rows and Cols

    -    First save it in your current Matlab directory

>>  inmat = xlsread('ToyInput2.xls')

(note:  this only reads in numerical part, with NaN's for missing values)

>>  [mnum, mtext] = xlsread('ToyInput2.xls')

(this puts the numerical part in "mnum" and the text part in "mtext")

>>  size(mnum)

>>  size(mtext)

>>  mean(mnum)

>>  mean(mnum,1)

>>  mean(mnum,2)

(Note:  the NaNs tend to "dominate operations")

>>  mnumpad = mnum

(create a copy for twiddling by "padding")

>>  mnumpad(3,3) = 0

>>  mnumpad(3,4) = 0

(artificially replace these by 0's, to avoid those problems)

>>  mean(mnumpad,1)

(problem:  putting 0's in can affect important operations)

>>  help stats

(there are some operations that "do the right things with missing values")

>>  nanmean(mnum)

>>  mean(mnumpad,1)

(however, these are not so sophisticated)

>>  help nanmean
 

Caution about "xlsread":

    -    Sometimes may need to save .xls files as "older versions"

    -    E.g. "Microsoft Excel 5.0/95 Workbook (*.xls)"
 
 


15. Matlab Script Files  (discussed 11/5/02)
 

Main Idea:

    -    A "list of Matlab commands"

    -    "Run the list" (i.e. make them all happen) by "calling" the filename

    -    Powerful way to "run many commands"

    -    Very useful to "remember what you did"
 

Approach:

    -    Create a "File"

    -    with "file suffix":    .m

    -    E.g. from Matlab:    File    --->    New    --->    M-file

(this starts the "Matlab File Editor")

(could just as well use another text file editor, e.g. Notepad)

    -    Each line of the file is a Matlab command

    -    After file is created, can (probably) just click on the filename, to edit

    -    Personal suggestion:    first line tells you what you are doing:

                disp('Running Matlab Script File ...')
 

Useful mode of operation:

    -    Type a few lines

    -    Then "run the script", by typing the file prefix in Matlab

    -    Look for errors

    -    Fix errors, and/or go on

    -    Useful to separate different parts by "if" blocks

("if" blocks need to finish with an "end" statement)

    -    useful to indent text within a block

(just makes it easier to see what is "inside")

    -    "else"  and "elseif" are also useful
 

Key concept:

    -    provides a record of what you did

    -    i.e. have "reproducible results"

    -    easy to make small twiddles later


16.    Kernel Density Estimation in Matlab

This is easy to do using my personal Matlab Files
 

To Download These:

    *    Get Files from here

    *    Could downloaded files individually

(using right click & "Save Link As" (Netscape) or "Save Target As" (Explorer))

    *    But more convenient to download all as single .zip file:

            -    I. e. download file:    AllSmoothingConmbined.zip

            -    Save file in a directory where you keep Matlab files

            -    "Unzip" the file to that directory

            -    This recreates those files in that directory
 

To Make These Files Accessible to Matlab:

    *    Most important is that above the directory appears in the Matlab "path"

    *    Try ">> path"    (shows the current "path")

(the set of directories that Matlab searches through to find functions)

    *    Try ">> help path"    (shows how the "path" command works)

    *    This suggest the very useful command "addpath"

(Understand this with ">> help addpath")

    *    Use "addpath" to put the directory with the files into the Matlab Path

    *    Check path is correct using ">> help kdeSM"

            -    If get help screen, then have correctly added the path

            -    If get error message, then path does not include the needed directory
 

To do Kernel Density Estimation:

    *    Load data into Matlab, e.g. as previously discussed here

    *    Initially try ">> kdeSM(data)"

(where "data" is a vector containing the data)

    *    Generally worth creating a "Script File", as previously discussed here

            -    Allows simple keeping track of what is done

            -    Allows simple reproducability

    *    Can twiddle many things, using "paramstruct"

            -    This is a "structure", find out more using ">> help struct"

            -    Note that ">> help kdeSM" gives instructions for creation of this

            -    Replace strings for "fields" with values in 1st column

            -    Use "values" as described in 2nd column

            -    For an example "script" go here

            -    The data used in that script are "Stamp Thicknesses", obtained here

(Do "Save As" to save these as files in an appropriate location on your computer)

            -    Data are thicknesses of Hidalgo Stamp

                    +    Produced in Mexico over 70 years in 1800s

                    +    Paper produced by several factories

                    +    Widely varying paper thicknesses (in mm)

                    +    From how many factories?

                    +    Statistical Literature:    Answers from 2 - 10!

                    +    But 7 is most common answer

    *    Also can twiddle some graph aspects using "Edit   --->   ___ Properties"
 
 


17.    More Graphics in Matlab

Font Sizes

Line Widths

Graphics Handles

Set command

Adding Lines, etc.  hold on ; hold off

Adding text
 
 


If you have suggestions for improving this page, please send email to marron@stat.unc.edu.
 

Back to Stat 6D Home Page
 

Back to Marron's Home Page