Top Banner
Three Dimensional Plots of Smoothed Bivariate Distributions • Smoothing using Rectangular Kernel • Preparing Data for Import to Mathematica • Starting and Using Mathematica • Importing Data • Three-D Surface Plots • Animations
23

Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Dec 29, 2015

Download

Documents

Madlyn Patrick
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Three Dimensional Plots of Smoothed Bivariate Distributions

• Smoothing using Rectangular Kernel

• Preparing Data for Import to Mathematica

• Starting and Using Mathematica

• Importing Data

• Three-D Surface Plots

• Animations

Page 2: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Smoothed Bivariate Distribution

• First we'll simulate some data.

tX <- c(rnorm(150, mean=10, sd=5), rnorm(50, mean=0, sd=2))

tY <- 5 + .5 * tX + rnorm(200,mean=0,sd=2)

Page 3: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

smoothX <- seq(-5, 25, by=2)

smoothY <- seq(-5, 25, by=2)

smoothF <- matrix(NA, length(smoothX), length(smoothY))

h <- 5

for (i in 1:length(smoothX)) {

x <- smoothX[i]

t1x <- abs(x - tX)/h

for (j in 1:length(smoothY)) {

y <- smoothY[j]

t1y <- abs(y - tY)/h

t2 <- rep(0, length(t1x))

t2[t1x < 1 & t1y < 1] <- 1/2

smoothF[i,j] <- sum(t2)/(length(tX)*h)

}

}

smoothF <- smoothF/sum(smoothF)

Page 4: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Preparing Data for Import to Mathematica

• Finally we'll write it to an ASCII textfile.

• Before running this line, substitute in your own pathname for your class directory.

write(round(t(smoothF),7),

"H:/Class/Psych344509/smoothF.dat",

ncolumns=dim(smoothF)[2])

Page 5: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Preparing Data for Import to Mathematica

• Now let's look at the data file in PFE

• Open smoothF.dat in PFE

0 0.0006098 0.001626 0.004065 0.0058943 0.0060976 0.0054878 0.0044715 0.0020325 0.0002033 0 0 0 0 0 0

0 0.0006098 0.002439 0.0069106 0.0097561 0.0101626 0.0095528 0.0077236 0.003252 0.0004065 0 0 0 0 0 0

0 0.0006098 0.002439 0.0077236 0.0113821 0.0119919 0.0115854 0.0097561 0.0044715 0.000813 0.0002033 0 0 0 0 0

Page 6: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Starting and Using Mathematica

• Locate and start Mathematica 4.1 in the Program Menu.

• There will be an new “notebook” waiting.

• Type 2+2; and press the Enter key on the keypad.

• The number 4 will appear as output.

• Note that the input and output from the first calculation is delimited by a square bracket on the right.

Page 7: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Starting and Using Mathematica

• Now, in the next input line type

Plot[Sin[x], {x, 0, 10}]

• Press Enter and you should see a nice plot of a sine wave evaluated from x= 0 to x=10.

Page 8: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.
Page 9: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Getting Help in Mathematica• In the next input cell type

?Plot

• Now press Enter and see what happens.

• You can also use the Help Browser.

• Help is arranged by "Packages".

• Some Packages are optional.

Page 10: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Getting Help in Mathematica

• Scroll down the page of help on Plot after clicking on “further examples”.

• You will see example code that you can copy and paste into your notebook.

Page 11: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Saving Your Work in Mathematica

• Choose the menu item File->SaveAs and save your work into your class folder as the filename "Example1".

• Save your work often when using Mathematica as it has a tendency to crash under Windows.

Page 12: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

More Help in Mathematica

• In the Help Browser, the left-most column should have Graphics selected.

• In the second column from the left, select 3D Plots.

• In the last column select ListPlot3D.

Page 13: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

More Help in Mathematica

• Evaluate the example to see a 3D plot.

• Now let’s try a 3D Plot

tMatrix = {{1,2,3}, {3,4,5}, {5,6,7}}

ListPlot3D[tMatrix]

• Type them in and press enter.

Page 14: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.
Page 15: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Importing Data into Mathematica• Open the file you downloaded at the

beginning of class: ThreeD3.nb• Notice that this file opens in a second

notebook.

• The first input cell of this file should read:<<Graphics`Animation`

• Put your cursor in that cell by clicking anywhere in it and then press Enter.

• This loads the Add-On animation package.

Page 16: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Viewing an Animation• Press enter in the second and third cells.

• You’ll see a lot of graphics being generated.

• These are the cels in an animation.

• Double-Click on one of the cels so that it is selected with a box around it.

• The graphic should start spinning.

• The controls for slowing or speeding the animation are at the bottom of the notebook window.

Page 17: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Importing Data into Mathematica• That was fun, but now let’s get down to

business.

• Click in the next input area after the animated graphics.

• Change the directory to be your class directory.

• Press Enter.

Page 18: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Three-D Surface Plots

• The next cell has a line that reads

ListPlot3D[theData1]

• Put your cursor in that line and press Enter.

• A 3-D projection of the surface we created in Splus will be plotted.

Page 19: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.
Page 20: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Three-D Surface Plots

• Let’s animate the plot with SpinShow.

• If you change the size of the first graph, it will change the size of the whole animation the next time you press Enter.

Page 21: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Three-D Surface Plots

• There are many options to 3D plotting for changing the perspective view, the hues, etc.

Page 22: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

Saving Graphs and Animations

• Click on a graph to select it.

• Select Edit -> Save Selection As

• Notice the options for saving the graphic or animation.

Page 23: Three Dimensional Plots of Smoothed Bivariate Distributions Smoothing using Rectangular Kernel Preparing Data for Import to Mathematica Starting and Using.

For Tuesday

• Next we'll talk about longitudinal data: time series, recursion visualization and state space plotting.