August 20, 2023
·
4 min read
Before the analysis, first convert the categorical variables low, race, smoke, ht, and ui in the birthwt dataset into factors.
library(MASS)
data(birthwt)
str(birthwt)
options(warn=-1)
library(dplyr)
birthwt <- birthwt %>%
mutate(low = factor(low, labels = c("no", "yes")),
race = factor(race, labels = c("white", "black", "other")),
smoke = factor(smoke, labels = c("no", "yes")),
ht = factor(ht, labels = c("no", "yes")),
ui = factor(ui, labels = c("no", "yes")))
str(birthwt)
Read the full text -> A total of 0.93k words
August 19, 2023
·
13 min read
1. Getting to Know the ggplot2 Package
#
The ggplot2 package provides a plotting system based on a grammar of layers. It remedies the lack of consistency among functions in R’s base graphics system and raises R’s plotting capabilities to an entirely new level. The basic principles of the various data visualizations in ggplot2 are completely consistent: it maps mathematical space to the space of graphical elements. Imagine a blank canvas. On the canvas, we need to define the data to be visualized (data) and the mapping from data variables to graphical properties (mapping).
Read the full text -> A total of 2.72k words
August 19, 2023
·
12 min read
R’s base graphics system was written by Ross Ihaka and is very powerful. It consists mainly of the graphics and grDevices packages, which are loaded automatically when R starts. There are two types of functions in the base graphics system: high-level plotting functions and low-level plotting functions.
High-level plotting functions are functions used to produce plots directly, including plot( ), hist( ), boxplot( ), and pairs( ). Low-level plotting functions are functions used to add new graphics or elements to a plot drawn by a high-level plotting function, including points( ), lines( ), text( ), title( ), legend( ), and axis( ).
Read the full text -> A total of 2.52k words
August 19, 2023
·
8 min read
Handling Missing Values
#
In actual data analysis, missing data are frequently encountered. Missing values usually occur because data were not collected or were not entered.
For example, a missing age may be due to someone not providing his (her) age. Most statistical analysis methods assume that complete datasets are being processed. Therefore, apart from some specialized books, most statistics textbooks rarely address this issue. In fact, before conducting a formal analysis, we need to check whether the dataset contains missing values during the data preparation stage and use some methods to compensate for the loss caused by missing values.
Read the full text -> A total of 1.74k words
August 19, 2023
·
4 min read
Sometimes datasets come from multiple places, and we need to merge two or more datasets into one dataset. Operations for merging data frames include vertical merging, horizontal merging, and merging by a shared variable.
Read the full text -> A total of 0.84k words
August 18, 2023
·
9 min read
This package handles data frames more efficiently with a unified specification. ** The first parameter of all functions that process data frames in the dplyr package is the data frame name. **
Taking the birthwt dataset in the MASS package as an example, the following describes the use of commonly used functions in the dplyr package. This data set comes from a case-control study of risk factors for low birth weight in newborns. Start by loading the dataset and viewing its related information.
library(dplyr)
data(birthwt, package = "MASS")
# ??birthwt
A total of 189 subjects and 10 variables were included in the dataset birthwt. where the outcome variable bwt is the weight of the newborn (unit: g) and the variable low is a binary classification variable that converts the value of bwt into 2500g points. The remaining 8 variables are predictors, including pregnant women’s age, race, smoking status, and history of hypertension (ht).
Read the full text -> A total of 1.85k words
August 17, 2023
·
10 min read
Using Base R
#
In actual data analysis, analysts often spend a lot of effort on data preparation, transforming data into the form required for analysis. Unfortunately, most statistics textbooks rarely address this important issue. Organizing data is one of the tasks of statistics. We started to focus on the most common data format in R - the basic operation of data frames. We will first process the data frames using the base package.
Load a small dataset Familydata in the epiDisplay package first.
library(epiDisplay)
data("Familydata")
Read the full text -> A total of 2.06k words
August 17, 2023
·
5 min read
In fact, R has a large number of built-in datasets available for analysis and practice, and we can also create data in R that simulate specific distributions. In actual work, however, data analysts more often face external data from various data sources, namely data files with all kinds of extensions, such as .txt, .csv, .xlsx, .xls, and so on. Files with different extensions represent different file formats, which often causes trouble for analysts.
R provides data import tools with a wide range of applications.
Read the full text -> A total of 1.10k words
August 17, 2023
·
20 min read
好的设计模式方便对代码进行功能扩展/修改,降低耦合性,提高复用性。
from abc import abstractmethod, ABC
from typing import Callable, final, Tuple, List, Sequence, Dict
from random import choices, randrange
from functools import wraps
from numpy import ndarray
from pandas import DataFrame
from time import perf_counter, sleep
from datetime import datetime, timedelta
Read the full text -> A total of 4.16k words
August 16, 2023
·
11 min read
The first step in any data analysis is to create a dataset in the required format. In R, this task consists of two steps: first select a data structure to store the data, and then enter or import the data into this data structure. The following introduces the various data structures used to store data in R.
Read the full text -> A total of 2.23k words