0% found this document useful (0 votes)
11 views8 pages

Profiling & Simulation Aim:: Pre-Lab Discussion Theory

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Profiling & Simulation Aim:: Pre-Lab Discussion Theory

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROFILING &

SIMULATION

AIM:

To write a program for simulation and profiling in R.

Pre-Lab Discussion

THEORY

They system.time() function takes an arbitrary R expression as input (can be wrapped


in curly braces) and returns the amount of time taken to evaluate the expression.
The system.time() function computes the time (in seconds) needed to execute an expression
and if there’s an error, gives the time until the error occurred. The function returns an object
of class proc_time which contains two useful bits of information:

 user time: time charged to the CPU(s) for this expression


 elapsed time: “wall clock” time, the amount of time that passes for you as you’re
sitting there
The Rprof() function starts the profiler in R

Rprof() keeps track of the function call stack at regularly sampled intervals and tabulates
how much time is spent inside each function. By default, the profiler samples the function
call stack every 0.02 seconds.

> Rprof() ## Turn on the profiler

> Rprof(NULL) ## Turn off the profiler

Rprof() function, everything that you do from then on will be measured by the
profiler. Therefore, you usually only want to run a single R function or expression once you
turn on the profiler and then immediately turn it off. The reason is that if you mix too many
function calls together when running the profiler, all of the results will be mixed together
and you won’t be able to sort out where the bottlenecks are. In reality, I usually only run a
single function with the profiler on.

 The raw output from the profiler looks something like this. Here I’m calling
the lm() function on some data with the profiler running.

 X<-rnorm(10)

 y<-rnorm(10)

 ## lm(y ~ x)

 sample.interval=10000
PROGRAM:
## Elapsed time > user time

system.time(readLines("http://www.jhsph.edu"))

## Elapsed time < user time

> hilbert <- function(n) {

+ i <- 1:n

+ 1 / outer(i - 1, i, "+")

+}

> x <- hilbert(1000)

> system.time(svd(x))

X<-rnorm(10)

y<-rnorm(10)

## lm(y ~ x)

sample.interval=10000

OUTPUT:

User system elapsed

0.004 0.002 0.431

User system elapsed

1.035 0.255 0.462


## Always set your seed!

set.seed(20)

## Simulate predictor variable

x <- rnorm(100)

## Simulate the error term e <- rnorm(100, 0, 2)

## Compute the outcome via the model

y <- 0.5 + 2 * x + e

summary(y)

Min. 1st Qu. Median Mean 3rd Qu. Max.

-6.4084 -1.5402 0.6789 0.6893 2.9303 6.5052

plot(x, y)

set.seed(10)

x <- rbinom(100, 1, 0.5)

str(x) ## 'x' is now 0s and 1s

int [1:100] 1 0 0 1 0 0 0 0 1 0 ...


e <- rnorm(100, 0, 2)

y <- 0.5 + 2 * x + e

plot(x, y)

RESULT:
Thus the program to perform SQL operation in R was successfully executed.
QUERY DATA USING SQL AND R

AIM:

To perform sql operations using R programming

ALGORITHM:

Step 1: at first we need to connect the mysql server to the r script by installing the
library called RMySQL.

Step 2: after connection we need to create the table using the dbSentQuery function.

Step 3: after creation of table in database we need to get values from the user to
insert into the sql table.

Step 3: then we need to insert the data into the table using dbSentQuery function.

Step 4: after insertion we need to get the values of the table from database using
dbGetQuery.

PROGRAM:

#install RMySQL and call the library

library(RMySQL)

#connect the mysql server to perform operations

con <- dbConnect(RMySQL::MySQL(),

dbname = "database_name",

host = "localhost",

port = 3306,

user =

"root"

#creation of table in the database

dbSendQuery(con,'create table stud(name varchar(30),mark1 int(10),mark2 int(10),mark3


int(10))')
while(TRUE){

val<-toupper(as.character(readline(prompt = "Do you want to add student data?:(S/N)")))

if(val=='S'){

#getting values from the user

name<- as.character(readline(prompt = "Enter student

name:")) mark1<-as.integer(readline(prompt = "Enter student

mark1:")) mark2<-as.integer(readline(prompt = "Enter student

mark2:")) mark3<-as.integer(readline(prompt = "Enter student

mark3:"))

#adding all the values to the database

dbSendQuery(con,paste("INSERT INTO `stud`(`name`, `mark1`, `mark2`, `mark3`)


VALUES ('",name,"','",mark1,"','",mark2,"','",mark3,"')"))

}else{

break

#getting all values of database->table

stud_db<-dbGetQuery(con,'select * from

stud') stud_db

OUTPUT:

Do you want to add student data?:(S/N)s

Enter student name:xxx

Enter student mark1:99

Enter student mark2:100


Enter student mark3:88

Do you want to add student data?:(S/N)s

Enter student name:yyy

Enter student mark1:88

Enter student mark2:89

Enter student mark3:78

Do you want to add student data?:(S/N)s

Enter student name:zzz

Enter student mark1:56

Enter student mark2:67

Enter student mark3:79

Do you want to add student data?:(S/N)n

#printing the table -> stud_db

name mark1 mark2 mark3

1 xxx 99 100 88

2 yyy 88 89 78

3 zzz 56 67 79
RESULT:

Thus the program to perform SQL operation in R was successfully executed.

You might also like